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