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