github.com/liujq9674git/golang-src-1.7@v0.0.0-20230517174348-17f6ec47f3f8/src/runtime/os_openbsd.go (about) 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 "runtime/internal/atomic" 9 "unsafe" 10 ) 11 12 type mOS struct { 13 waitsemacount uint32 14 } 15 16 //go:noescape 17 func setitimer(mode int32, new, old *itimerval) 18 19 //go:noescape 20 func sigaction(sig int32, new, old *sigactiont) 21 22 //go:noescape 23 func sigaltstack(new, old *stackt) 24 25 //go:noescape 26 func sigprocmask(mode int32, new sigset) sigset 27 28 //go:noescape 29 func sysctl(mib *uint32, miblen uint32, out *byte, size *uintptr, dst *byte, ndst uintptr) int32 30 31 func raise(sig int32) 32 func raiseproc(sig int32) 33 34 //go:noescape 35 func tfork(param *tforkt, psize uintptr, mm *m, gg *g, fn uintptr) int32 36 37 //go:noescape 38 func thrsleep(ident uintptr, clock_id int32, tsp *timespec, lock uintptr, abort *uint32) int32 39 40 //go:noescape 41 func thrwakeup(ident uintptr, n int32) int32 42 43 func osyield() 44 45 const ( 46 _ESRCH = 3 47 _EAGAIN = 35 48 _EWOULDBLOCK = _EAGAIN 49 _ENOTSUP = 91 50 51 // From OpenBSD's sys/time.h 52 _CLOCK_REALTIME = 0 53 _CLOCK_VIRTUAL = 1 54 _CLOCK_PROF = 2 55 _CLOCK_MONOTONIC = 3 56 ) 57 58 type sigset uint32 59 60 const ( 61 sigset_none = sigset(0) 62 sigset_all = ^sigset(0) 63 ) 64 65 // From OpenBSD's <sys/sysctl.h> 66 const ( 67 _CTL_HW = 6 68 _HW_NCPU = 3 69 ) 70 71 func getncpu() int32 { 72 mib := [2]uint32{_CTL_HW, _HW_NCPU} 73 out := uint32(0) 74 nout := unsafe.Sizeof(out) 75 76 // Fetch hw.ncpu via sysctl. 77 ret := sysctl(&mib[0], 2, (*byte)(unsafe.Pointer(&out)), &nout, nil, 0) 78 if ret >= 0 { 79 return int32(out) 80 } 81 return 1 82 } 83 84 //go:nosplit 85 func semacreate(mp *m) { 86 } 87 88 //go:nosplit 89 func semasleep(ns int64) int32 { 90 _g_ := getg() 91 92 // Compute sleep deadline. 93 var tsp *timespec 94 if ns >= 0 { 95 var ts timespec 96 var nsec int32 97 ns += nanotime() 98 ts.set_sec(int64(timediv(ns, 1000000000, &nsec))) 99 ts.set_nsec(nsec) 100 tsp = &ts 101 } 102 103 for { 104 v := atomic.Load(&_g_.m.waitsemacount) 105 if v > 0 { 106 if atomic.Cas(&_g_.m.waitsemacount, v, v-1) { 107 return 0 // semaphore acquired 108 } 109 continue 110 } 111 112 // Sleep until woken by semawakeup or timeout; or abort if waitsemacount != 0. 113 // 114 // From OpenBSD's __thrsleep(2) manual: 115 // "The abort argument, if not NULL, points to an int that will 116 // be examined [...] immediately before blocking. If that int 117 // is non-zero then __thrsleep() will immediately return EINTR 118 // without blocking." 119 ret := thrsleep(uintptr(unsafe.Pointer(&_g_.m.waitsemacount)), _CLOCK_MONOTONIC, tsp, 0, &_g_.m.waitsemacount) 120 if ret == _EWOULDBLOCK { 121 return -1 122 } 123 } 124 } 125 126 //go:nosplit 127 func semawakeup(mp *m) { 128 atomic.Xadd(&mp.waitsemacount, 1) 129 ret := thrwakeup(uintptr(unsafe.Pointer(&mp.waitsemacount)), 1) 130 if ret != 0 && ret != _ESRCH { 131 // semawakeup can be called on signal stack. 132 systemstack(func() { 133 print("thrwakeup addr=", &mp.waitsemacount, " sem=", mp.waitsemacount, " ret=", ret, "\n") 134 }) 135 } 136 } 137 138 // May run with m.p==nil, so write barriers are not allowed. 139 //go:nowritebarrier 140 func newosproc(mp *m, stk unsafe.Pointer) { 141 if false { 142 print("newosproc stk=", stk, " m=", mp, " g=", mp.g0, " id=", mp.id, " ostk=", &mp, "\n") 143 } 144 145 param := tforkt{ 146 tf_tcb: unsafe.Pointer(&mp.tls[0]), 147 tf_tid: (*int32)(unsafe.Pointer(&mp.procid)), 148 tf_stack: uintptr(stk), 149 } 150 151 oset := sigprocmask(_SIG_SETMASK, sigset_all) 152 ret := tfork(¶m, unsafe.Sizeof(param), mp, mp.g0, funcPC(mstart)) 153 sigprocmask(_SIG_SETMASK, oset) 154 155 if ret < 0 { 156 print("runtime: failed to create new OS thread (have ", mcount()-1, " already; errno=", -ret, ")\n") 157 if ret == -_EAGAIN { 158 println("runtime: may need to increase max user processes (ulimit -p)") 159 } 160 throw("runtime.newosproc") 161 } 162 } 163 164 func osinit() { 165 ncpu = getncpu() 166 } 167 168 var urandom_dev = []byte("/dev/urandom\x00") 169 170 //go:nosplit 171 func getRandomData(r []byte) { 172 fd := open(&urandom_dev[0], 0 /* O_RDONLY */, 0) 173 n := read(fd, unsafe.Pointer(&r[0]), int32(len(r))) 174 closefd(fd) 175 extendRandom(r, int(n)) 176 } 177 178 func goenvs() { 179 goenvs_unix() 180 } 181 182 // Called to initialize a new m (including the bootstrap m). 183 // Called on the parent thread (main thread in case of bootstrap), can allocate memory. 184 func mpreinit(mp *m) { 185 mp.gsignal = malg(32 * 1024) 186 mp.gsignal.m = mp 187 } 188 189 //go:nosplit 190 func msigsave(mp *m) { 191 mp.sigmask = sigprocmask(_SIG_BLOCK, 0) 192 } 193 194 //go:nosplit 195 func msigrestore(sigmask sigset) { 196 sigprocmask(_SIG_SETMASK, sigmask) 197 } 198 199 //go:nosplit 200 func sigblock() { 201 sigprocmask(_SIG_SETMASK, sigset_all) 202 } 203 204 // Called to initialize a new m (including the bootstrap m). 205 // Called on the new thread, can not allocate memory. 206 func minit() { 207 _g_ := getg() 208 209 // m.procid is a uint64, but tfork writes an int32. Fix it up. 210 _g_.m.procid = uint64(*(*int32)(unsafe.Pointer(&_g_.m.procid))) 211 212 // Initialize signal handling 213 var st stackt 214 sigaltstack(nil, &st) 215 if st.ss_flags&_SS_DISABLE != 0 { 216 signalstack(&_g_.m.gsignal.stack) 217 _g_.m.newSigstack = true 218 } else { 219 // Use existing signal stack. 220 stsp := uintptr(unsafe.Pointer(st.ss_sp)) 221 _g_.m.gsignal.stack.lo = stsp 222 _g_.m.gsignal.stack.hi = stsp + st.ss_size 223 _g_.m.gsignal.stackguard0 = stsp + _StackGuard 224 _g_.m.gsignal.stackguard1 = stsp + _StackGuard 225 _g_.m.gsignal.stackAlloc = st.ss_size 226 _g_.m.newSigstack = false 227 } 228 229 // restore signal mask from m.sigmask and unblock essential signals 230 nmask := _g_.m.sigmask 231 for i := range sigtable { 232 if sigtable[i].flags&_SigUnblock != 0 { 233 nmask &^= 1 << (uint32(i) - 1) 234 } 235 } 236 sigprocmask(_SIG_SETMASK, nmask) 237 } 238 239 // Called from dropm to undo the effect of an minit. 240 //go:nosplit 241 func unminit() { 242 if getg().m.newSigstack { 243 signalstack(nil) 244 } 245 } 246 247 func memlimit() uintptr { 248 return 0 249 } 250 251 func sigtramp() 252 253 type sigactiont struct { 254 sa_sigaction uintptr 255 sa_mask uint32 256 sa_flags int32 257 } 258 259 //go:nosplit 260 //go:nowritebarrierrec 261 func setsig(i int32, fn uintptr, restart bool) { 262 var sa sigactiont 263 sa.sa_flags = _SA_SIGINFO | _SA_ONSTACK 264 if restart { 265 sa.sa_flags |= _SA_RESTART 266 } 267 sa.sa_mask = uint32(sigset_all) 268 if fn == funcPC(sighandler) { 269 fn = funcPC(sigtramp) 270 } 271 sa.sa_sigaction = fn 272 sigaction(i, &sa, nil) 273 } 274 275 //go:nosplit 276 //go:nowritebarrierrec 277 func setsigstack(i int32) { 278 throw("setsigstack") 279 } 280 281 //go:nosplit 282 //go:nowritebarrierrec 283 func getsig(i int32) uintptr { 284 var sa sigactiont 285 sigaction(i, nil, &sa) 286 if sa.sa_sigaction == funcPC(sigtramp) { 287 return funcPC(sighandler) 288 } 289 return sa.sa_sigaction 290 } 291 292 //go:nosplit 293 func signalstack(s *stack) { 294 var st stackt 295 if s == nil { 296 st.ss_flags = _SS_DISABLE 297 } else { 298 st.ss_sp = s.lo 299 st.ss_size = s.hi - s.lo 300 st.ss_flags = 0 301 } 302 sigaltstack(&st, nil) 303 } 304 305 //go:nosplit 306 //go:nowritebarrierrec 307 func updatesigmask(m sigmask) { 308 sigprocmask(_SIG_SETMASK, sigset(m[0])) 309 } 310 311 func unblocksig(sig int32) { 312 mask := sigset(1) << (uint32(sig) - 1) 313 sigprocmask(_SIG_UNBLOCK, mask) 314 }