github.com/aloncn/graphics-go@v0.0.1/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(sigmask sigset) {
   127  	sigprocmask(_SIG_SETMASK, &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  
   145  	// On DragonFly a thread created by pthread_create inherits
   146  	// the signal stack of the creating thread.  We always create
   147  	// a new signal stack here, to avoid having two Go threads
   148  	// using the same signal stack.  This breaks the case of a
   149  	// thread created in C that calls sigaltstack and then calls a
   150  	// Go function, because we will lose track of the C code's
   151  	// sigaltstack, but it's the best we can do.
   152  	signalstack(&_g_.m.gsignal.stack)
   153  	_g_.m.newSigstack = true
   154  
   155  	// restore signal mask from m.sigmask and unblock essential signals
   156  	nmask := _g_.m.sigmask
   157  	for i := range sigtable {
   158  		if sigtable[i].flags&_SigUnblock != 0 {
   159  			nmask.__bits[(i-1)/32] &^= 1 << ((uint32(i) - 1) & 31)
   160  		}
   161  	}
   162  	sigprocmask(_SIG_SETMASK, &nmask, nil)
   163  }
   164  
   165  // Called from dropm to undo the effect of an minit.
   166  //go:nosplit
   167  func unminit() {
   168  	if getg().m.newSigstack {
   169  		signalstack(nil)
   170  	}
   171  }
   172  
   173  func memlimit() uintptr {
   174  	/*
   175  		                TODO: Convert to Go when something actually uses the result.
   176  
   177  				Rlimit rl;
   178  				extern byte runtime·text[], runtime·end[];
   179  				uintptr used;
   180  
   181  				if(runtime·getrlimit(RLIMIT_AS, &rl) != 0)
   182  					return 0;
   183  				if(rl.rlim_cur >= 0x7fffffff)
   184  					return 0;
   185  
   186  				// Estimate our VM footprint excluding the heap.
   187  				// Not an exact science: use size of binary plus
   188  				// some room for thread stacks.
   189  				used = runtime·end - runtime·text + (64<<20);
   190  				if(used >= rl.rlim_cur)
   191  					return 0;
   192  
   193  				// If there's not at least 16 MB left, we're probably
   194  				// not going to be able to do much.  Treat as no limit.
   195  				rl.rlim_cur -= used;
   196  				if(rl.rlim_cur < (16<<20))
   197  					return 0;
   198  
   199  				return rl.rlim_cur - used;
   200  	*/
   201  	return 0
   202  }
   203  
   204  func sigtramp()
   205  
   206  type sigactiont struct {
   207  	sa_sigaction uintptr
   208  	sa_flags     int32
   209  	sa_mask      sigset
   210  }
   211  
   212  //go:nosplit
   213  //go:nowritebarrierrec
   214  func setsig(i int32, fn uintptr, restart bool) {
   215  	var sa sigactiont
   216  	sa.sa_flags = _SA_SIGINFO | _SA_ONSTACK
   217  	if restart {
   218  		sa.sa_flags |= _SA_RESTART
   219  	}
   220  	sa.sa_mask = sigset_all
   221  	if fn == funcPC(sighandler) {
   222  		fn = funcPC(sigtramp)
   223  	}
   224  	sa.sa_sigaction = fn
   225  	sigaction(i, &sa, nil)
   226  }
   227  
   228  //go:nosplit
   229  //go:nowritebarrierrec
   230  func setsigstack(i int32) {
   231  	throw("setsigstack")
   232  }
   233  
   234  //go:nosplit
   235  //go:nowritebarrierrec
   236  func getsig(i int32) uintptr {
   237  	var sa sigactiont
   238  	sigaction(i, nil, &sa)
   239  	if sa.sa_sigaction == funcPC(sigtramp) {
   240  		return funcPC(sighandler)
   241  	}
   242  	return sa.sa_sigaction
   243  }
   244  
   245  //go:nosplit
   246  func signalstack(s *stack) {
   247  	var st sigaltstackt
   248  	if s == nil {
   249  		st.ss_flags = _SS_DISABLE
   250  	} else {
   251  		st.ss_sp = s.lo
   252  		st.ss_size = s.hi - s.lo
   253  		st.ss_flags = 0
   254  	}
   255  	sigaltstack(&st, nil)
   256  }
   257  
   258  //go:nosplit
   259  //go:nowritebarrierrec
   260  func updatesigmask(m sigmask) {
   261  	var mask sigset
   262  	copy(mask.__bits[:], m[:])
   263  	sigprocmask(_SIG_SETMASK, &mask, nil)
   264  }
   265  
   266  func unblocksig(sig int32) {
   267  	var mask sigset
   268  	mask.__bits[(sig-1)/32] |= 1 << ((uint32(sig) - 1) & 31)
   269  	sigprocmask(_SIG_UNBLOCK, &mask, nil)
   270  }