github.com/mh-cbon/go@v0.0.0-20160603070303-9e112a3fe4c0/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 throw("runtime.newosproc") 158 } 159 } 160 161 func osinit() { 162 ncpu = getncpu() 163 } 164 165 var urandom_dev = []byte("/dev/urandom\x00") 166 167 //go:nosplit 168 func getRandomData(r []byte) { 169 fd := open(&urandom_dev[0], 0 /* O_RDONLY */, 0) 170 n := read(fd, unsafe.Pointer(&r[0]), int32(len(r))) 171 closefd(fd) 172 extendRandom(r, int(n)) 173 } 174 175 func goenvs() { 176 goenvs_unix() 177 } 178 179 // Called to initialize a new m (including the bootstrap m). 180 // Called on the parent thread (main thread in case of bootstrap), can allocate memory. 181 func mpreinit(mp *m) { 182 mp.gsignal = malg(32 * 1024) 183 mp.gsignal.m = mp 184 } 185 186 //go:nosplit 187 func msigsave(mp *m) { 188 mp.sigmask = sigprocmask(_SIG_BLOCK, 0) 189 } 190 191 //go:nosplit 192 func msigrestore(sigmask sigset) { 193 sigprocmask(_SIG_SETMASK, sigmask) 194 } 195 196 //go:nosplit 197 func sigblock() { 198 sigprocmask(_SIG_SETMASK, sigset_all) 199 } 200 201 // Called to initialize a new m (including the bootstrap m). 202 // Called on the new thread, can not allocate memory. 203 func minit() { 204 _g_ := getg() 205 206 // m.procid is a uint64, but tfork writes an int32. Fix it up. 207 _g_.m.procid = uint64(*(*int32)(unsafe.Pointer(&_g_.m.procid))) 208 209 // Initialize signal handling 210 var st stackt 211 sigaltstack(nil, &st) 212 if st.ss_flags&_SS_DISABLE != 0 { 213 signalstack(&_g_.m.gsignal.stack) 214 _g_.m.newSigstack = true 215 } else { 216 // Use existing signal stack. 217 stsp := uintptr(unsafe.Pointer(st.ss_sp)) 218 _g_.m.gsignal.stack.lo = stsp 219 _g_.m.gsignal.stack.hi = stsp + st.ss_size 220 _g_.m.gsignal.stackguard0 = stsp + _StackGuard 221 _g_.m.gsignal.stackguard1 = stsp + _StackGuard 222 _g_.m.gsignal.stackAlloc = st.ss_size 223 _g_.m.newSigstack = false 224 } 225 226 // restore signal mask from m.sigmask and unblock essential signals 227 nmask := _g_.m.sigmask 228 for i := range sigtable { 229 if sigtable[i].flags&_SigUnblock != 0 { 230 nmask &^= 1 << (uint32(i) - 1) 231 } 232 } 233 sigprocmask(_SIG_SETMASK, nmask) 234 } 235 236 // Called from dropm to undo the effect of an minit. 237 //go:nosplit 238 func unminit() { 239 if getg().m.newSigstack { 240 signalstack(nil) 241 } 242 } 243 244 func memlimit() uintptr { 245 return 0 246 } 247 248 func sigtramp() 249 250 type sigactiont struct { 251 sa_sigaction uintptr 252 sa_mask uint32 253 sa_flags int32 254 } 255 256 //go:nosplit 257 //go:nowritebarrierrec 258 func setsig(i int32, fn uintptr, restart bool) { 259 var sa sigactiont 260 sa.sa_flags = _SA_SIGINFO | _SA_ONSTACK 261 if restart { 262 sa.sa_flags |= _SA_RESTART 263 } 264 sa.sa_mask = uint32(sigset_all) 265 if fn == funcPC(sighandler) { 266 fn = funcPC(sigtramp) 267 } 268 sa.sa_sigaction = fn 269 sigaction(i, &sa, nil) 270 } 271 272 //go:nosplit 273 //go:nowritebarrierrec 274 func setsigstack(i int32) { 275 throw("setsigstack") 276 } 277 278 //go:nosplit 279 //go:nowritebarrierrec 280 func getsig(i int32) uintptr { 281 var sa sigactiont 282 sigaction(i, nil, &sa) 283 if sa.sa_sigaction == funcPC(sigtramp) { 284 return funcPC(sighandler) 285 } 286 return sa.sa_sigaction 287 } 288 289 //go:nosplit 290 func signalstack(s *stack) { 291 var st stackt 292 if s == nil { 293 st.ss_flags = _SS_DISABLE 294 } else { 295 st.ss_sp = s.lo 296 st.ss_size = s.hi - s.lo 297 st.ss_flags = 0 298 } 299 sigaltstack(&st, nil) 300 } 301 302 //go:nosplit 303 //go:nowritebarrierrec 304 func updatesigmask(m sigmask) { 305 sigprocmask(_SIG_SETMASK, sigset(m[0])) 306 } 307 308 func unblocksig(sig int32) { 309 mask := sigset(1) << (uint32(sig) - 1) 310 sigprocmask(_SIG_UNBLOCK, mask) 311 }