github.com/reiver/go@v0.0.0-20150109200633-1d0c7792f172/src/runtime/os1_linux.go (about)

     1  // Copyright 2009 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  var sigset_none sigset
    10  var sigset_all sigset = sigset{^uint32(0), ^uint32(0)}
    11  
    12  // Linux futex.
    13  //
    14  //	futexsleep(uint32 *addr, uint32 val)
    15  //	futexwakeup(uint32 *addr)
    16  //
    17  // Futexsleep atomically checks if *addr == val and if so, sleeps on addr.
    18  // Futexwakeup wakes up threads sleeping on addr.
    19  // Futexsleep is allowed to wake up spuriously.
    20  
    21  const (
    22  	_FUTEX_WAIT = 0
    23  	_FUTEX_WAKE = 1
    24  )
    25  
    26  // Atomically,
    27  //	if(*addr == val) sleep
    28  // Might be woken up spuriously; that's allowed.
    29  // Don't sleep longer than ns; ns < 0 means forever.
    30  //go:nosplit
    31  func futexsleep(addr *uint32, val uint32, ns int64) {
    32  	var ts timespec
    33  
    34  	// Some Linux kernels have a bug where futex of
    35  	// FUTEX_WAIT returns an internal error code
    36  	// as an errno.  Libpthread ignores the return value
    37  	// here, and so can we: as it says a few lines up,
    38  	// spurious wakeups are allowed.
    39  	if ns < 0 {
    40  		futex(unsafe.Pointer(addr), _FUTEX_WAIT, val, nil, nil, 0)
    41  		return
    42  	}
    43  
    44  	// It's difficult to live within the no-split stack limits here.
    45  	// On ARM and 386, a 64-bit divide invokes a general software routine
    46  	// that needs more stack than we can afford. So we use timediv instead.
    47  	// But on real 64-bit systems, where words are larger but the stack limit
    48  	// is not, even timediv is too heavy, and we really need to use just an
    49  	// ordinary machine instruction.
    50  	if ptrSize == 8 {
    51  		ts.set_sec(ns / 1000000000)
    52  		ts.set_nsec(int32(ns % 1000000000))
    53  	} else {
    54  		ts.tv_nsec = 0
    55  		ts.set_sec(int64(timediv(ns, 1000000000, (*int32)(unsafe.Pointer(&ts.tv_nsec)))))
    56  	}
    57  	futex(unsafe.Pointer(addr), _FUTEX_WAIT, val, unsafe.Pointer(&ts), nil, 0)
    58  }
    59  
    60  // If any procs are sleeping on addr, wake up at most cnt.
    61  //go:nosplit
    62  func futexwakeup(addr *uint32, cnt uint32) {
    63  	ret := futex(unsafe.Pointer(addr), _FUTEX_WAKE, cnt, nil, nil, 0)
    64  	if ret >= 0 {
    65  		return
    66  	}
    67  
    68  	// I don't know that futex wakeup can return
    69  	// EAGAIN or EINTR, but if it does, it would be
    70  	// safe to loop and call futex again.
    71  	systemstack(func() {
    72  		print("futexwakeup addr=", addr, " returned ", ret, "\n")
    73  	})
    74  
    75  	*(*int32)(unsafe.Pointer(uintptr(0x1006))) = 0x1006
    76  }
    77  
    78  func getproccount() int32 {
    79  	var buf [16]uintptr
    80  	r := sched_getaffinity(0, unsafe.Sizeof(buf), &buf[0])
    81  	n := int32(0)
    82  	for _, v := range buf[:r/ptrSize] {
    83  		for i := 0; i < 64; i++ {
    84  			n += int32(v & 1)
    85  			v >>= 1
    86  		}
    87  	}
    88  	if n == 0 {
    89  		n = 1
    90  	}
    91  	return n
    92  }
    93  
    94  // Clone, the Linux rfork.
    95  const (
    96  	_CLONE_VM             = 0x100
    97  	_CLONE_FS             = 0x200
    98  	_CLONE_FILES          = 0x400
    99  	_CLONE_SIGHAND        = 0x800
   100  	_CLONE_PTRACE         = 0x2000
   101  	_CLONE_VFORK          = 0x4000
   102  	_CLONE_PARENT         = 0x8000
   103  	_CLONE_THREAD         = 0x10000
   104  	_CLONE_NEWNS          = 0x20000
   105  	_CLONE_SYSVSEM        = 0x40000
   106  	_CLONE_SETTLS         = 0x80000
   107  	_CLONE_PARENT_SETTID  = 0x100000
   108  	_CLONE_CHILD_CLEARTID = 0x200000
   109  	_CLONE_UNTRACED       = 0x800000
   110  	_CLONE_CHILD_SETTID   = 0x1000000
   111  	_CLONE_STOPPED        = 0x2000000
   112  	_CLONE_NEWUTS         = 0x4000000
   113  	_CLONE_NEWIPC         = 0x8000000
   114  )
   115  
   116  func newosproc(mp *m, stk unsafe.Pointer) {
   117  	/*
   118  	 * note: strace gets confused if we use CLONE_PTRACE here.
   119  	 */
   120  	var flags int32 = _CLONE_VM | /* share memory */
   121  		_CLONE_FS | /* share cwd, etc */
   122  		_CLONE_FILES | /* share fd table */
   123  		_CLONE_SIGHAND | /* share sig handler table */
   124  		_CLONE_THREAD /* revisit - okay for now */
   125  
   126  	mp.tls[0] = uintptr(mp.id) // so 386 asm can find it
   127  	if false {
   128  		print("newosproc stk=", stk, " m=", mp, " g=", mp.g0, " clone=", funcPC(clone), " id=", mp.id, "/", mp.tls[0], " ostk=", &mp, "\n")
   129  	}
   130  
   131  	// Disable signals during clone, so that the new thread starts
   132  	// with signals disabled.  It will enable them in minit.
   133  	var oset sigset
   134  	rtsigprocmask(_SIG_SETMASK, &sigset_all, &oset, int32(unsafe.Sizeof(oset)))
   135  	ret := clone(flags, stk, unsafe.Pointer(mp), unsafe.Pointer(mp.g0), unsafe.Pointer(funcPC(mstart)))
   136  	rtsigprocmask(_SIG_SETMASK, &oset, nil, int32(unsafe.Sizeof(oset)))
   137  
   138  	if ret < 0 {
   139  		print("runtime: failed to create new OS thread (have ", mcount(), " already; errno=", -ret, ")\n")
   140  		throw("newosproc")
   141  	}
   142  }
   143  
   144  func osinit() {
   145  	ncpu = getproccount()
   146  }
   147  
   148  var urandom_dev = []byte("/dev/urandom\x00")
   149  
   150  func getRandomData(r []byte) {
   151  	if startupRandomData != nil {
   152  		n := copy(r, startupRandomData)
   153  		extendRandom(r, n)
   154  		return
   155  	}
   156  	fd := open(&urandom_dev[0], 0 /* O_RDONLY */, 0)
   157  	n := read(fd, unsafe.Pointer(&r[0]), int32(len(r)))
   158  	close(fd)
   159  	extendRandom(r, int(n))
   160  }
   161  
   162  func goenvs() {
   163  	goenvs_unix()
   164  }
   165  
   166  // Called to initialize a new m (including the bootstrap m).
   167  // Called on the parent thread (main thread in case of bootstrap), can allocate memory.
   168  func mpreinit(mp *m) {
   169  	mp.gsignal = malg(32 * 1024) // Linux wants >= 2K
   170  	mp.gsignal.m = mp
   171  }
   172  
   173  // Called to initialize a new m (including the bootstrap m).
   174  // Called on the new thread, can not allocate memory.
   175  func minit() {
   176  	// Initialize signal handling.
   177  	_g_ := getg()
   178  	signalstack((*byte)(unsafe.Pointer(_g_.m.gsignal.stack.lo)), 32*1024)
   179  	rtsigprocmask(_SIG_SETMASK, &sigset_none, nil, int32(unsafe.Sizeof(sigset_none)))
   180  }
   181  
   182  // Called from dropm to undo the effect of an minit.
   183  func unminit() {
   184  	signalstack(nil, 0)
   185  }
   186  
   187  func memlimit() uintptr {
   188  	/*
   189  		TODO: Convert to Go when something actually uses the result.
   190  
   191  		Rlimit rl;
   192  		extern byte runtime·text[], runtime·end[];
   193  		uintptr used;
   194  
   195  		if(runtime·getrlimit(RLIMIT_AS, &rl) != 0)
   196  			return 0;
   197  		if(rl.rlim_cur >= 0x7fffffff)
   198  			return 0;
   199  
   200  		// Estimate our VM footprint excluding the heap.
   201  		// Not an exact science: use size of binary plus
   202  		// some room for thread stacks.
   203  		used = runtime·end - runtime·text + (64<<20);
   204  		if(used >= rl.rlim_cur)
   205  			return 0;
   206  
   207  		// If there's not at least 16 MB left, we're probably
   208  		// not going to be able to do much.  Treat as no limit.
   209  		rl.rlim_cur -= used;
   210  		if(rl.rlim_cur < (16<<20))
   211  			return 0;
   212  
   213  		return rl.rlim_cur - used;
   214  	*/
   215  
   216  	return 0
   217  }
   218  
   219  //#ifdef GOARCH_386
   220  //#define sa_handler k_sa_handler
   221  //#endif
   222  
   223  func sigreturn()
   224  func sigtramp()
   225  
   226  func setsig(i int32, fn uintptr, restart bool) {
   227  	var sa sigactiont
   228  	memclr(unsafe.Pointer(&sa), unsafe.Sizeof(sa))
   229  	sa.sa_flags = _SA_SIGINFO | _SA_ONSTACK | _SA_RESTORER
   230  	if restart {
   231  		sa.sa_flags |= _SA_RESTART
   232  	}
   233  	sa.sa_mask = ^uint64(0)
   234  	// Although Linux manpage says "sa_restorer element is obsolete and
   235  	// should not be used". x86_64 kernel requires it. Only use it on
   236  	// x86.
   237  	if GOARCH == "386" || GOARCH == "amd64" {
   238  		sa.sa_restorer = funcPC(sigreturn)
   239  	}
   240  	if fn == funcPC(sighandler) {
   241  		fn = funcPC(sigtramp)
   242  	}
   243  	sa.sa_handler = fn
   244  	if rt_sigaction(uintptr(i), &sa, nil, unsafe.Sizeof(sa.sa_mask)) != 0 {
   245  		throw("rt_sigaction failure")
   246  	}
   247  }
   248  
   249  func setsigstack(i int32) {
   250  	var sa sigactiont
   251  	if rt_sigaction(uintptr(i), nil, &sa, unsafe.Sizeof(sa.sa_mask)) != 0 {
   252  		throw("rt_sigaction failure")
   253  	}
   254  	if sa.sa_handler == 0 || sa.sa_handler == _SIG_DFL || sa.sa_handler == _SIG_IGN || sa.sa_flags&_SA_ONSTACK != 0 {
   255  		return
   256  	}
   257  	sa.sa_flags |= _SA_ONSTACK
   258  	if rt_sigaction(uintptr(i), &sa, nil, unsafe.Sizeof(sa.sa_mask)) != 0 {
   259  		throw("rt_sigaction failure")
   260  	}
   261  }
   262  
   263  func getsig(i int32) uintptr {
   264  	var sa sigactiont
   265  
   266  	memclr(unsafe.Pointer(&sa), unsafe.Sizeof(sa))
   267  	if rt_sigaction(uintptr(i), nil, &sa, unsafe.Sizeof(sa.sa_mask)) != 0 {
   268  		throw("rt_sigaction read failure")
   269  	}
   270  	if sa.sa_handler == funcPC(sigtramp) {
   271  		return funcPC(sighandler)
   272  	}
   273  	return sa.sa_handler
   274  }
   275  
   276  func signalstack(p *byte, n int32) {
   277  	var st sigaltstackt
   278  	st.ss_sp = p
   279  	st.ss_size = uintptr(n)
   280  	st.ss_flags = 0
   281  	if p == nil {
   282  		st.ss_flags = _SS_DISABLE
   283  	}
   284  	sigaltstack(&st, nil)
   285  }
   286  
   287  func unblocksignals() {
   288  	rtsigprocmask(_SIG_SETMASK, &sigset_none, nil, int32(unsafe.Sizeof(sigset_none)))
   289  }