github.com/reiver/go@v0.0.0-20150109200633-1d0c7792f172/src/runtime/os1_freebsd.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 FreeBSD'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  // FreeBSD's umtx_op syscall is effectively the same as Linux's futex, and
    30  // thus the code is largely similar. See Linux implementation
    31  // and lock_futex.c for comments.
    32  
    33  //go:nosplit
    34  func futexsleep(addr *uint32, val uint32, ns int64) {
    35  	systemstack(func() {
    36  		futexsleep1(addr, val, ns)
    37  	})
    38  }
    39  
    40  func futexsleep1(addr *uint32, val uint32, ns int64) {
    41  	var tsp *timespec
    42  	if ns >= 0 {
    43  		var ts timespec
    44  		ts.tv_nsec = 0
    45  		ts.set_sec(int64(timediv(ns, 1000000000, (*int32)(unsafe.Pointer(&ts.tv_nsec)))))
    46  		tsp = &ts
    47  	}
    48  	ret := sys_umtx_op(addr, _UMTX_OP_WAIT_UINT_PRIVATE, val, nil, tsp)
    49  	if ret >= 0 || ret == -_EINTR {
    50  		return
    51  	}
    52  	print("umtx_wait addr=", addr, " val=", val, " ret=", ret, "\n")
    53  	*(*int32)(unsafe.Pointer(uintptr(0x1005))) = 0x1005
    54  }
    55  
    56  //go:nosplit
    57  func futexwakeup(addr *uint32, cnt uint32) {
    58  	ret := sys_umtx_op(addr, _UMTX_OP_WAKE_PRIVATE, cnt, nil, nil)
    59  	if ret >= 0 {
    60  		return
    61  	}
    62  
    63  	systemstack(func() {
    64  		print("umtx_wake_addr=", addr, " ret=", ret, "\n")
    65  	})
    66  }
    67  
    68  func thr_start()
    69  
    70  func newosproc(mp *m, stk unsafe.Pointer) {
    71  	if false {
    72  		print("newosproc stk=", stk, " m=", mp, " g=", mp.g0, " thr_start=", funcPC(thr_start), " id=", mp.id, "/", mp.tls[0], " ostk=", &mp, "\n")
    73  	}
    74  
    75  	// NOTE(rsc): This code is confused. stackbase is the top of the stack
    76  	// and is equal to stk. However, it's working, so I'm not changing it.
    77  	param := thrparam{
    78  		start_func: funcPC(thr_start),
    79  		arg:        unsafe.Pointer(mp),
    80  		stack_base: mp.g0.stack.hi,
    81  		stack_size: uintptr(stk) - mp.g0.stack.hi,
    82  		child_tid:  unsafe.Pointer(&mp.procid),
    83  		parent_tid: nil,
    84  		tls_base:   unsafe.Pointer(&mp.tls[0]),
    85  		tls_size:   unsafe.Sizeof(mp.tls),
    86  	}
    87  	mp.tls[0] = uintptr(mp.id) // so 386 asm can find it
    88  
    89  	var oset sigset
    90  	sigprocmask(&sigset_all, &oset)
    91  	thr_new(&param, int32(unsafe.Sizeof(param)))
    92  	sigprocmask(&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  	close(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  // Called to initialize a new m (including the bootstrap m).
   121  // Called on the new thread, can not allocate memory.
   122  func minit() {
   123  	_g_ := getg()
   124  
   125  	// m.procid is a uint64, but thr_new writes a uint32 on 32-bit systems.
   126  	// Fix it up. (Only matters on big-endian, but be clean anyway.)
   127  	if ptrSize == 4 {
   128  		_g_.m.procid = uint64(*(*uint32)(unsafe.Pointer(&_g_.m.procid)))
   129  	}
   130  
   131  	// Initialize signal handling.
   132  	signalstack((*byte)(unsafe.Pointer(_g_.m.gsignal.stack.lo)), 32*1024)
   133  	sigprocmask(&sigset_none, nil)
   134  }
   135  
   136  // Called from dropm to undo the effect of an minit.
   137  func unminit() {
   138  	signalstack(nil, 0)
   139  }
   140  
   141  func memlimit() uintptr {
   142  	/*
   143  		TODO: Convert to Go when something actually uses the result.
   144  		Rlimit rl;
   145  		extern byte runtime·text[], runtime·end[];
   146  		uintptr used;
   147  
   148  		if(runtime·getrlimit(RLIMIT_AS, &rl) != 0)
   149  			return 0;
   150  		if(rl.rlim_cur >= 0x7fffffff)
   151  			return 0;
   152  
   153  		// Estimate our VM footprint excluding the heap.
   154  		// Not an exact science: use size of binary plus
   155  		// some room for thread stacks.
   156  		used = runtime·end - runtime·text + (64<<20);
   157  		if(used >= rl.rlim_cur)
   158  			return 0;
   159  
   160  		// If there's not at least 16 MB left, we're probably
   161  		// not going to be able to do much.  Treat as no limit.
   162  		rl.rlim_cur -= used;
   163  		if(rl.rlim_cur < (16<<20))
   164  			return 0;
   165  
   166  		return rl.rlim_cur - used;
   167  	*/
   168  
   169  	return 0
   170  }
   171  
   172  func sigtramp()
   173  
   174  type sigactiont struct {
   175  	sa_handler uintptr
   176  	sa_flags   int32
   177  	sa_mask    sigset
   178  }
   179  
   180  func setsig(i int32, fn uintptr, restart bool) {
   181  	var sa sigactiont
   182  	sa.sa_flags = _SA_SIGINFO | _SA_ONSTACK
   183  	if restart {
   184  		sa.sa_flags |= _SA_RESTART
   185  	}
   186  	sa.sa_mask = sigset_all
   187  	if fn == funcPC(sighandler) {
   188  		fn = funcPC(sigtramp)
   189  	}
   190  	sa.sa_handler = fn
   191  	sigaction(i, &sa, nil)
   192  }
   193  
   194  func setsigstack(i int32) {
   195  	throw("setsigstack")
   196  }
   197  
   198  func getsig(i int32) uintptr {
   199  	var sa sigactiont
   200  	sigaction(i, nil, &sa)
   201  	if sa.sa_handler == funcPC(sigtramp) {
   202  		return funcPC(sighandler)
   203  	}
   204  	return sa.sa_handler
   205  }
   206  
   207  func signalstack(p *byte, n int32) {
   208  	var st stackt
   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  }