github.com/fjballest/golang@v0.0.0-20151209143359-e4c5fe594ca8/src/runtime/os1_dragonfly.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 "unsafe"
     8  
     9  // From DragonFly's <sys/sysctl.h>
    10  const (
    11  	_CTL_HW  = 6
    12  	_HW_NCPU = 3
    13  )
    14  
    15  var sigset_all = sigset{[4]uint32{^uint32(0), ^uint32(0), ^uint32(0), ^uint32(0)}}
    16  
    17  func getncpu() int32 {
    18  	mib := [2]uint32{_CTL_HW, _HW_NCPU}
    19  	out := uint32(0)
    20  	nout := unsafe.Sizeof(out)
    21  	ret := sysctl(&mib[0], 2, (*byte)(unsafe.Pointer(&out)), &nout, nil, 0)
    22  	if ret >= 0 {
    23  		return int32(out)
    24  	}
    25  	return 1
    26  }
    27  
    28  //go:nosplit
    29  func futexsleep(addr *uint32, val uint32, ns int64) {
    30  	systemstack(func() {
    31  		futexsleep1(addr, val, ns)
    32  	})
    33  }
    34  
    35  func futexsleep1(addr *uint32, val uint32, ns int64) {
    36  	var timeout int32
    37  	if ns >= 0 {
    38  		// The timeout is specified in microseconds - ensure that we
    39  		// do not end up dividing to zero, which would put us to sleep
    40  		// indefinitely...
    41  		timeout = timediv(ns, 1000, nil)
    42  		if timeout == 0 {
    43  			timeout = 1
    44  		}
    45  	}
    46  
    47  	// sys_umtx_sleep will return EWOULDBLOCK (EAGAIN) when the timeout
    48  	// expires or EBUSY if the mutex value does not match.
    49  	ret := sys_umtx_sleep(addr, int32(val), timeout)
    50  	if ret >= 0 || ret == -_EINTR || ret == -_EAGAIN || ret == -_EBUSY {
    51  		return
    52  	}
    53  
    54  	print("umtx_sleep addr=", addr, " val=", val, " ret=", ret, "\n")
    55  	*(*int32)(unsafe.Pointer(uintptr(0x1005))) = 0x1005
    56  }
    57  
    58  //go:nosplit
    59  func futexwakeup(addr *uint32, cnt uint32) {
    60  	ret := sys_umtx_wakeup(addr, int32(cnt))
    61  	if ret >= 0 {
    62  		return
    63  	}
    64  
    65  	systemstack(func() {
    66  		print("umtx_wake_addr=", addr, " ret=", ret, "\n")
    67  		*(*int32)(unsafe.Pointer(uintptr(0x1006))) = 0x1006
    68  	})
    69  }
    70  
    71  func lwp_start(uintptr)
    72  
    73  // May run with m.p==nil, so write barriers are not allowed.
    74  //go:nowritebarrier
    75  func newosproc(mp *m, stk unsafe.Pointer) {
    76  	if false {
    77  		print("newosproc stk=", stk, " m=", mp, " g=", mp.g0, " lwp_start=", funcPC(lwp_start), " id=", mp.id, " ostk=", &mp, "\n")
    78  	}
    79  
    80  	var oset sigset
    81  	sigprocmask(_SIG_SETMASK, &sigset_all, &oset)
    82  
    83  	params := lwpparams{
    84  		start_func: funcPC(lwp_start),
    85  		arg:        unsafe.Pointer(mp),
    86  		stack:      uintptr(stk),
    87  		tid1:       unsafe.Pointer(&mp.procid),
    88  		tid2:       nil,
    89  	}
    90  
    91  	lwp_create(&params)
    92  	sigprocmask(_SIG_SETMASK, &oset, nil)
    93  }
    94  
    95  func osinit() {
    96  	ncpu = getncpu()
    97  }
    98  
    99  var urandom_dev = []byte("/dev/urandom\x00")
   100  
   101  //go:nosplit
   102  func getRandomData(r []byte) {
   103  	fd := open(&urandom_dev[0], 0 /* O_RDONLY */, 0)
   104  	n := read(fd, unsafe.Pointer(&r[0]), int32(len(r)))
   105  	closefd(fd)
   106  	extendRandom(r, int(n))
   107  }
   108  
   109  func goenvs() {
   110  	goenvs_unix()
   111  }
   112  
   113  // Called to initialize a new m (including the bootstrap m).
   114  // Called on the parent thread (main thread in case of bootstrap), can allocate memory.
   115  func mpreinit(mp *m) {
   116  	mp.gsignal = malg(32 * 1024)
   117  	mp.gsignal.m = mp
   118  }
   119  
   120  //go:nosplit
   121  func msigsave(mp *m) {
   122  	sigprocmask(_SIG_SETMASK, nil, &mp.sigmask)
   123  }
   124  
   125  //go:nosplit
   126  func msigrestore(mp *m) {
   127  	sigprocmask(_SIG_SETMASK, &mp.sigmask, nil)
   128  }
   129  
   130  //go:nosplit
   131  func sigblock() {
   132  	sigprocmask(_SIG_SETMASK, &sigset_all, nil)
   133  }
   134  
   135  // Called to initialize a new m (including the bootstrap m).
   136  // Called on the new thread, can not allocate memory.
   137  func minit() {
   138  	_g_ := getg()
   139  
   140  	// m.procid is a uint64, but lwp_start writes an int32. Fix it up.
   141  	_g_.m.procid = uint64(*(*int32)(unsafe.Pointer(&_g_.m.procid)))
   142  
   143  	// Initialize signal handling
   144  	signalstack(&_g_.m.gsignal.stack)
   145  
   146  	// restore signal mask from m.sigmask and unblock essential signals
   147  	nmask := _g_.m.sigmask
   148  	for i := range sigtable {
   149  		if sigtable[i].flags&_SigUnblock != 0 {
   150  			nmask.__bits[(i-1)/32] &^= 1 << ((uint32(i) - 1) & 31)
   151  		}
   152  	}
   153  	sigprocmask(_SIG_SETMASK, &nmask, nil)
   154  }
   155  
   156  // Called from dropm to undo the effect of an minit.
   157  func unminit() {
   158  	signalstack(nil)
   159  }
   160  
   161  func memlimit() uintptr {
   162  	/*
   163  		                TODO: Convert to Go when something actually uses the result.
   164  
   165  				Rlimit rl;
   166  				extern byte runtime·text[], runtime·end[];
   167  				uintptr used;
   168  
   169  				if(runtime·getrlimit(RLIMIT_AS, &rl) != 0)
   170  					return 0;
   171  				if(rl.rlim_cur >= 0x7fffffff)
   172  					return 0;
   173  
   174  				// Estimate our VM footprint excluding the heap.
   175  				// Not an exact science: use size of binary plus
   176  				// some room for thread stacks.
   177  				used = runtime·end - runtime·text + (64<<20);
   178  				if(used >= rl.rlim_cur)
   179  					return 0;
   180  
   181  				// If there's not at least 16 MB left, we're probably
   182  				// not going to be able to do much.  Treat as no limit.
   183  				rl.rlim_cur -= used;
   184  				if(rl.rlim_cur < (16<<20))
   185  					return 0;
   186  
   187  				return rl.rlim_cur - used;
   188  	*/
   189  	return 0
   190  }
   191  
   192  func sigtramp()
   193  
   194  type sigactiont struct {
   195  	sa_sigaction uintptr
   196  	sa_flags     int32
   197  	sa_mask      sigset
   198  }
   199  
   200  func setsig(i int32, fn uintptr, restart bool) {
   201  	var sa sigactiont
   202  	sa.sa_flags = _SA_SIGINFO | _SA_ONSTACK
   203  	if restart {
   204  		sa.sa_flags |= _SA_RESTART
   205  	}
   206  	sa.sa_mask = sigset_all
   207  	if fn == funcPC(sighandler) {
   208  		fn = funcPC(sigtramp)
   209  	}
   210  	sa.sa_sigaction = fn
   211  	sigaction(i, &sa, nil)
   212  }
   213  
   214  func setsigstack(i int32) {
   215  	throw("setsigstack")
   216  }
   217  
   218  func getsig(i int32) uintptr {
   219  	var sa sigactiont
   220  	sigaction(i, nil, &sa)
   221  	if sa.sa_sigaction == funcPC(sigtramp) {
   222  		return funcPC(sighandler)
   223  	}
   224  	return sa.sa_sigaction
   225  }
   226  
   227  //go:nosplit
   228  func signalstack(s *stack) {
   229  	var st sigaltstackt
   230  	if s == nil {
   231  		st.ss_flags = _SS_DISABLE
   232  	} else {
   233  		st.ss_sp = s.lo
   234  		st.ss_size = s.hi - s.lo
   235  		st.ss_flags = 0
   236  	}
   237  	sigaltstack(&st, nil)
   238  }
   239  
   240  func updatesigmask(m sigmask) {
   241  	var mask sigset
   242  	copy(mask.__bits[:], m[:])
   243  	sigprocmask(_SIG_SETMASK, &mask, nil)
   244  }
   245  
   246  func unblocksig(sig int32) {
   247  	var mask sigset
   248  	mask.__bits[(sig-1)/32] |= 1 << ((uint32(sig) - 1) & 31)
   249  	sigprocmask(_SIG_UNBLOCK, &mask, nil)
   250  }