github.com/goproxy0/go@v0.0.0-20171111080102-49cc0c489d2c/src/runtime/os_netbsd.go (about)

     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  	"runtime/internal/atomic"
     9  	"unsafe"
    10  )
    11  
    12  const (
    13  	_SS_DISABLE  = 4
    14  	_SIG_BLOCK   = 1
    15  	_SIG_UNBLOCK = 2
    16  	_SIG_SETMASK = 3
    17  	_NSIG        = 33
    18  	_SI_USER     = 0
    19  
    20  	// From NetBSD's <sys/ucontext.h>
    21  	_UC_SIGMASK = 0x01
    22  	_UC_CPU     = 0x04
    23  
    24  	// From <sys/lwp.h>
    25  	_LWP_DETACHED = 0x00000040
    26  
    27  	_EAGAIN = 35
    28  )
    29  
    30  type mOS struct {
    31  	waitsemacount uint32
    32  }
    33  
    34  //go:noescape
    35  func setitimer(mode int32, new, old *itimerval)
    36  
    37  //go:noescape
    38  func sigaction(sig uint32, new, old *sigactiont)
    39  
    40  //go:noescape
    41  func sigaltstack(new, old *stackt)
    42  
    43  //go:noescape
    44  func sigprocmask(how int32, new, old *sigset)
    45  
    46  //go:noescape
    47  func sysctl(mib *uint32, miblen uint32, out *byte, size *uintptr, dst *byte, ndst uintptr) int32
    48  
    49  func lwp_tramp()
    50  
    51  func raise(sig uint32)
    52  func raiseproc(sig uint32)
    53  
    54  //go:noescape
    55  func getcontext(ctxt unsafe.Pointer)
    56  
    57  //go:noescape
    58  func lwp_create(ctxt unsafe.Pointer, flags uintptr, lwpid unsafe.Pointer) int32
    59  
    60  //go:noescape
    61  func lwp_park(abstime *timespec, unpark int32, hint, unparkhint unsafe.Pointer) int32
    62  
    63  //go:noescape
    64  func lwp_unpark(lwp int32, hint unsafe.Pointer) int32
    65  
    66  func lwp_self() int32
    67  
    68  func osyield()
    69  
    70  const (
    71  	_ESRCH     = 3
    72  	_ETIMEDOUT = 60
    73  
    74  	// From NetBSD's <sys/time.h>
    75  	_CLOCK_REALTIME  = 0
    76  	_CLOCK_VIRTUAL   = 1
    77  	_CLOCK_PROF      = 2
    78  	_CLOCK_MONOTONIC = 3
    79  )
    80  
    81  var sigset_all = sigset{[4]uint32{^uint32(0), ^uint32(0), ^uint32(0), ^uint32(0)}}
    82  
    83  // From NetBSD's <sys/sysctl.h>
    84  const (
    85  	_CTL_HW      = 6
    86  	_HW_NCPU     = 3
    87  	_HW_PAGESIZE = 7
    88  )
    89  
    90  func getncpu() int32 {
    91  	mib := [2]uint32{_CTL_HW, _HW_NCPU}
    92  	out := uint32(0)
    93  	nout := unsafe.Sizeof(out)
    94  	ret := sysctl(&mib[0], 2, (*byte)(unsafe.Pointer(&out)), &nout, nil, 0)
    95  	if ret >= 0 {
    96  		return int32(out)
    97  	}
    98  	return 1
    99  }
   100  
   101  func getPageSize() uintptr {
   102  	mib := [2]uint32{_CTL_HW, _HW_PAGESIZE}
   103  	out := uint32(0)
   104  	nout := unsafe.Sizeof(out)
   105  	ret := sysctl(&mib[0], 2, (*byte)(unsafe.Pointer(&out)), &nout, nil, 0)
   106  	if ret >= 0 {
   107  		return uintptr(out)
   108  	}
   109  	return 0
   110  }
   111  
   112  //go:nosplit
   113  func semacreate(mp *m) {
   114  }
   115  
   116  //go:nosplit
   117  func semasleep(ns int64) int32 {
   118  	_g_ := getg()
   119  
   120  	// Compute sleep deadline.
   121  	var tsp *timespec
   122  	if ns >= 0 {
   123  		var ts timespec
   124  		var nsec int32
   125  		ns += nanotime()
   126  		ts.set_sec(timediv(ns, 1000000000, &nsec))
   127  		ts.set_nsec(nsec)
   128  		tsp = &ts
   129  	}
   130  
   131  	for {
   132  		v := atomic.Load(&_g_.m.waitsemacount)
   133  		if v > 0 {
   134  			if atomic.Cas(&_g_.m.waitsemacount, v, v-1) {
   135  				return 0 // semaphore acquired
   136  			}
   137  			continue
   138  		}
   139  
   140  		// Sleep until unparked by semawakeup or timeout.
   141  		ret := lwp_park(tsp, 0, unsafe.Pointer(&_g_.m.waitsemacount), nil)
   142  		if ret == _ETIMEDOUT {
   143  			return -1
   144  		}
   145  	}
   146  }
   147  
   148  //go:nosplit
   149  func semawakeup(mp *m) {
   150  	atomic.Xadd(&mp.waitsemacount, 1)
   151  	// From NetBSD's _lwp_unpark(2) manual:
   152  	// "If the target LWP is not currently waiting, it will return
   153  	// immediately upon the next call to _lwp_park()."
   154  	ret := lwp_unpark(int32(mp.procid), unsafe.Pointer(&mp.waitsemacount))
   155  	if ret != 0 && ret != _ESRCH {
   156  		// semawakeup can be called on signal stack.
   157  		systemstack(func() {
   158  			print("thrwakeup addr=", &mp.waitsemacount, " sem=", mp.waitsemacount, " ret=", ret, "\n")
   159  		})
   160  	}
   161  }
   162  
   163  // May run with m.p==nil, so write barriers are not allowed.
   164  //go:nowritebarrier
   165  func newosproc(mp *m, stk unsafe.Pointer) {
   166  	if false {
   167  		print("newosproc stk=", stk, " m=", mp, " g=", mp.g0, " id=", mp.id, " ostk=", &mp, "\n")
   168  	}
   169  
   170  	var uc ucontextt
   171  	getcontext(unsafe.Pointer(&uc))
   172  
   173  	// _UC_SIGMASK does not seem to work here.
   174  	// It would be nice if _UC_SIGMASK and _UC_STACK
   175  	// worked so that we could do all the work setting
   176  	// the sigmask and the stack here, instead of setting
   177  	// the mask here and the stack in netbsdMstart.
   178  	// For now do the blocking manually.
   179  	uc.uc_flags = _UC_SIGMASK | _UC_CPU
   180  	uc.uc_link = nil
   181  	uc.uc_sigmask = sigset_all
   182  
   183  	var oset sigset
   184  	sigprocmask(_SIG_SETMASK, &sigset_all, &oset)
   185  
   186  	lwp_mcontext_init(&uc.uc_mcontext, stk, mp, mp.g0, funcPC(netbsdMstart))
   187  
   188  	ret := lwp_create(unsafe.Pointer(&uc), _LWP_DETACHED, unsafe.Pointer(&mp.procid))
   189  	sigprocmask(_SIG_SETMASK, &oset, nil)
   190  	if ret < 0 {
   191  		print("runtime: failed to create new OS thread (have ", mcount()-1, " already; errno=", -ret, ")\n")
   192  		if ret == -_EAGAIN {
   193  			println("runtime: may need to increase max user processes (ulimit -p)")
   194  		}
   195  		throw("runtime.newosproc")
   196  	}
   197  }
   198  
   199  // netbsdMStart is the function call that starts executing a newly
   200  // created thread. On NetBSD, a new thread inherits the signal stack
   201  // of the creating thread. That confuses minit, so we remove that
   202  // signal stack here before calling the regular mstart. It's a bit
   203  // baroque to remove a signal stack here only to add one in minit, but
   204  // it's a simple change that keeps NetBSD working like other OS's.
   205  // At this point all signals are blocked, so there is no race.
   206  //go:nosplit
   207  func netbsdMstart() {
   208  	st := stackt{ss_flags: _SS_DISABLE}
   209  	sigaltstack(&st, nil)
   210  	mstart()
   211  }
   212  
   213  func osinit() {
   214  	ncpu = getncpu()
   215  	physPageSize = getPageSize()
   216  }
   217  
   218  var urandom_dev = []byte("/dev/urandom\x00")
   219  
   220  //go:nosplit
   221  func getRandomData(r []byte) {
   222  	fd := open(&urandom_dev[0], 0 /* O_RDONLY */, 0)
   223  	n := read(fd, unsafe.Pointer(&r[0]), int32(len(r)))
   224  	closefd(fd)
   225  	extendRandom(r, int(n))
   226  }
   227  
   228  func goenvs() {
   229  	goenvs_unix()
   230  }
   231  
   232  // Called to initialize a new m (including the bootstrap m).
   233  // Called on the parent thread (main thread in case of bootstrap), can allocate memory.
   234  func mpreinit(mp *m) {
   235  	mp.gsignal = malg(32 * 1024)
   236  	mp.gsignal.m = mp
   237  }
   238  
   239  // Called to initialize a new m (including the bootstrap m).
   240  // Called on the new thread, cannot allocate memory.
   241  func minit() {
   242  	_g_ := getg()
   243  	_g_.m.procid = uint64(lwp_self())
   244  
   245  	// On NetBSD a thread created by pthread_create inherits the
   246  	// signal stack of the creating thread. We always create a
   247  	// new signal stack here, to avoid having two Go threads using
   248  	// the same signal stack. This breaks the case of a thread
   249  	// created in C that calls sigaltstack and then calls a Go
   250  	// function, because we will lose track of the C code's
   251  	// sigaltstack, but it's the best we can do.
   252  	signalstack(&_g_.m.gsignal.stack)
   253  	_g_.m.newSigstack = true
   254  
   255  	minitSignalMask()
   256  }
   257  
   258  // Called from dropm to undo the effect of an minit.
   259  //go:nosplit
   260  func unminit() {
   261  	unminitSignals()
   262  }
   263  
   264  func memlimit() uintptr {
   265  	return 0
   266  }
   267  
   268  func sigtramp()
   269  
   270  type sigactiont struct {
   271  	sa_sigaction uintptr
   272  	sa_mask      sigset
   273  	sa_flags     int32
   274  }
   275  
   276  //go:nosplit
   277  //go:nowritebarrierrec
   278  func setsig(i uint32, fn uintptr) {
   279  	var sa sigactiont
   280  	sa.sa_flags = _SA_SIGINFO | _SA_ONSTACK | _SA_RESTART
   281  	sa.sa_mask = sigset_all
   282  	if fn == funcPC(sighandler) {
   283  		fn = funcPC(sigtramp)
   284  	}
   285  	sa.sa_sigaction = fn
   286  	sigaction(i, &sa, nil)
   287  }
   288  
   289  //go:nosplit
   290  //go:nowritebarrierrec
   291  func setsigstack(i uint32) {
   292  	throw("setsigstack")
   293  }
   294  
   295  //go:nosplit
   296  //go:nowritebarrierrec
   297  func getsig(i uint32) uintptr {
   298  	var sa sigactiont
   299  	sigaction(i, nil, &sa)
   300  	return sa.sa_sigaction
   301  }
   302  
   303  // setSignaltstackSP sets the ss_sp field of a stackt.
   304  //go:nosplit
   305  func setSignalstackSP(s *stackt, sp uintptr) {
   306  	s.ss_sp = sp
   307  }
   308  
   309  //go:nosplit
   310  //go:nowritebarrierrec
   311  func sigaddset(mask *sigset, i int) {
   312  	mask.__bits[(i-1)/32] |= 1 << ((uint32(i) - 1) & 31)
   313  }
   314  
   315  func sigdelset(mask *sigset, i int) {
   316  	mask.__bits[(i-1)/32] &^= 1 << ((uint32(i) - 1) & 31)
   317  }
   318  
   319  func (c *sigctxt) fixsigcode(sig uint32) {
   320  }