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