2014-11-13 19:01:12 -07:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package runtime
|
|
|
|
|
2015-11-02 12:09:24 -07:00
|
|
|
import (
|
|
|
|
"runtime/internal/atomic"
|
|
|
|
"unsafe"
|
|
|
|
)
|
2014-11-13 19:01:12 -07:00
|
|
|
|
2016-05-06 09:26:37 -06:00
|
|
|
type mOS struct {
|
|
|
|
waitsemacount uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
//go:noescape
|
|
|
|
func setitimer(mode int32, new, old *itimerval)
|
|
|
|
|
|
|
|
//go:noescape
|
|
|
|
func sigaction(sig int32, new, old *sigactiont)
|
|
|
|
|
|
|
|
//go:noescape
|
|
|
|
func sigaltstack(new, old *stackt)
|
|
|
|
|
|
|
|
//go:noescape
|
2016-09-23 18:54:51 -06:00
|
|
|
func obsdsigprocmask(how int32, new sigset) sigset
|
|
|
|
|
|
|
|
//go:nosplit
|
|
|
|
//go:nowritebarrierrec
|
|
|
|
func sigprocmask(how int32, new, old *sigset) {
|
|
|
|
n := sigset(0)
|
|
|
|
if new != nil {
|
|
|
|
n = *new
|
|
|
|
}
|
|
|
|
r := obsdsigprocmask(how, n)
|
|
|
|
if old != nil {
|
|
|
|
*old = r
|
|
|
|
}
|
|
|
|
}
|
2016-05-06 09:26:37 -06:00
|
|
|
|
|
|
|
//go:noescape
|
|
|
|
func sysctl(mib *uint32, miblen uint32, out *byte, size *uintptr, dst *byte, ndst uintptr) int32
|
|
|
|
|
|
|
|
func raise(sig int32)
|
|
|
|
func raiseproc(sig int32)
|
|
|
|
|
|
|
|
//go:noescape
|
|
|
|
func tfork(param *tforkt, psize uintptr, mm *m, gg *g, fn uintptr) int32
|
|
|
|
|
|
|
|
//go:noescape
|
|
|
|
func thrsleep(ident uintptr, clock_id int32, tsp *timespec, lock uintptr, abort *uint32) int32
|
|
|
|
|
|
|
|
//go:noescape
|
|
|
|
func thrwakeup(ident uintptr, n int32) int32
|
|
|
|
|
|
|
|
func osyield()
|
|
|
|
|
2014-11-13 19:01:12 -07:00
|
|
|
const (
|
2015-02-16 02:18:13 -07:00
|
|
|
_ESRCH = 3
|
|
|
|
_EAGAIN = 35
|
|
|
|
_EWOULDBLOCK = _EAGAIN
|
|
|
|
_ENOTSUP = 91
|
2014-11-13 19:01:12 -07:00
|
|
|
|
|
|
|
// From OpenBSD's sys/time.h
|
2015-02-16 02:18:13 -07:00
|
|
|
_CLOCK_REALTIME = 0
|
|
|
|
_CLOCK_VIRTUAL = 1
|
|
|
|
_CLOCK_PROF = 2
|
|
|
|
_CLOCK_MONOTONIC = 3
|
2014-11-13 19:01:12 -07:00
|
|
|
)
|
|
|
|
|
runtime: use a proper type, sigset, for m.sigmask
Replace the cross platform but unsafe [4]uintptr type with a OS
specific type, sigset. Most OSes already define sigset, and this
change defines a suitable sigset for the OSes that don't (darwin,
openbsd). The OSes that don't use m.sigmask (windows, plan9, nacl)
now defines sigset as the empty type, struct{}.
The gain is strongly typed access to m.sigmask, saving a dynamic
size sanity check and unsafe.Pointer casting. Also, some storage is
saved for each M, since [4]uinptr was conservative for most OSes.
The cost is that OSes that don't need m.sigmask has to define sigset.
completes ./all.bash with GOOS linux, on amd64
completes ./make.bash with GOOSes openbsd, android, plan9, windows,
darwin, solaris, netbsd, freebsd, dragonfly, all amd64.
With GOOS=nacl ./make.bash failed with a seemingly unrelated error.
[Replay of CL 16942 by Elias Naur.]
Change-Id: I98f144d626033ae5318576115ed635415ac71b2c
Reviewed-on: https://go-review.googlesource.com/17033
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
2015-11-17 03:41:06 -07:00
|
|
|
type sigset uint32
|
|
|
|
|
2016-09-23 18:54:51 -06:00
|
|
|
var sigset_all = ^sigset(0)
|
2014-11-13 19:01:12 -07:00
|
|
|
|
|
|
|
// From OpenBSD's <sys/sysctl.h>
|
|
|
|
const (
|
2016-07-18 19:40:02 -06:00
|
|
|
_CTL_HW = 6
|
|
|
|
_HW_NCPU = 3
|
|
|
|
_HW_PAGESIZE = 7
|
2014-11-13 19:01:12 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func getncpu() int32 {
|
2015-02-16 02:18:13 -07:00
|
|
|
mib := [2]uint32{_CTL_HW, _HW_NCPU}
|
2014-11-13 19:01:12 -07:00
|
|
|
out := uint32(0)
|
|
|
|
nout := unsafe.Sizeof(out)
|
|
|
|
|
|
|
|
// Fetch hw.ncpu via sysctl.
|
|
|
|
ret := sysctl(&mib[0], 2, (*byte)(unsafe.Pointer(&out)), &nout, nil, 0)
|
|
|
|
if ret >= 0 {
|
|
|
|
return int32(out)
|
|
|
|
}
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2016-07-18 19:40:02 -06:00
|
|
|
func getPageSize() uintptr {
|
|
|
|
mib := [2]uint32{_CTL_HW, _HW_PAGESIZE}
|
|
|
|
out := uint32(0)
|
|
|
|
nout := unsafe.Sizeof(out)
|
|
|
|
ret := sysctl(&mib[0], 2, (*byte)(unsafe.Pointer(&out)), &nout, nil, 0)
|
|
|
|
if ret >= 0 {
|
|
|
|
return uintptr(out)
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2014-11-13 19:01:12 -07:00
|
|
|
//go:nosplit
|
2015-10-21 19:36:05 -06:00
|
|
|
func semacreate(mp *m) {
|
2014-11-13 19:01:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//go:nosplit
|
|
|
|
func semasleep(ns int64) int32 {
|
|
|
|
_g_ := getg()
|
|
|
|
|
|
|
|
// Compute sleep deadline.
|
|
|
|
var tsp *timespec
|
|
|
|
if ns >= 0 {
|
|
|
|
var ts timespec
|
|
|
|
var nsec int32
|
|
|
|
ns += nanotime()
|
2014-11-14 12:50:00 -07:00
|
|
|
ts.set_sec(int64(timediv(ns, 1000000000, &nsec)))
|
2014-11-13 19:01:12 -07:00
|
|
|
ts.set_nsec(nsec)
|
|
|
|
tsp = &ts
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
2015-11-02 12:09:24 -07:00
|
|
|
v := atomic.Load(&_g_.m.waitsemacount)
|
2015-02-23 01:05:30 -07:00
|
|
|
if v > 0 {
|
2015-11-02 12:09:24 -07:00
|
|
|
if atomic.Cas(&_g_.m.waitsemacount, v, v-1) {
|
2015-02-23 01:05:30 -07:00
|
|
|
return 0 // semaphore acquired
|
2014-11-13 19:01:12 -07:00
|
|
|
}
|
2015-02-23 01:05:30 -07:00
|
|
|
continue
|
2014-11-13 19:01:12 -07:00
|
|
|
}
|
|
|
|
|
2015-02-23 01:05:30 -07:00
|
|
|
// Sleep until woken by semawakeup or timeout; or abort if waitsemacount != 0.
|
|
|
|
//
|
|
|
|
// From OpenBSD's __thrsleep(2) manual:
|
|
|
|
// "The abort argument, if not NULL, points to an int that will
|
2016-03-01 16:21:55 -07:00
|
|
|
// be examined [...] immediately before blocking. If that int
|
2015-02-23 01:05:30 -07:00
|
|
|
// is non-zero then __thrsleep() will immediately return EINTR
|
|
|
|
// without blocking."
|
|
|
|
ret := thrsleep(uintptr(unsafe.Pointer(&_g_.m.waitsemacount)), _CLOCK_MONOTONIC, tsp, 0, &_g_.m.waitsemacount)
|
2015-02-16 02:18:13 -07:00
|
|
|
if ret == _EWOULDBLOCK {
|
2014-11-13 19:01:12 -07:00
|
|
|
return -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//go:nosplit
|
|
|
|
func semawakeup(mp *m) {
|
2015-11-02 12:09:24 -07:00
|
|
|
atomic.Xadd(&mp.waitsemacount, 1)
|
2014-11-13 19:01:12 -07:00
|
|
|
ret := thrwakeup(uintptr(unsafe.Pointer(&mp.waitsemacount)), 1)
|
2015-02-16 02:18:13 -07:00
|
|
|
if ret != 0 && ret != _ESRCH {
|
2014-11-13 19:01:12 -07:00
|
|
|
// semawakeup can be called on signal stack.
|
|
|
|
systemstack(func() {
|
|
|
|
print("thrwakeup addr=", &mp.waitsemacount, " sem=", mp.waitsemacount, " ret=", ret, "\n")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-29 08:20:54 -06:00
|
|
|
// May run with m.p==nil, so write barriers are not allowed.
|
runtime: disallow write barriers in handoffp and callees
handoffp by definition runs without a P, so it's not allowed to have
write barriers. It doesn't have any right now, but mark it
nowritebarrier to disallow any creeping in in the future. handoffp in
turns calls startm, newm, and newosproc, all of which are "below Go"
and make sense to run without a P, so disallow write barriers in these
as well.
For most functions, we've done this because they may race with
stoptheworld() and hence must not have write barriers. For these
functions, it's a little different: the world can't stop while we're
in handoffp, so this race isn't present. But we implement this
restriction with a somewhat broader rule that you can't have a write
barrier without a P. We like this rule because it's simple and means
that our write barriers can depend on there being a P, even though
this rule is actually a little broader than necessary. Hence, even
though there's no danger of the race in these functions, we want to
adhere to the broader rule.
Change-Id: Ie22319c30eea37d703eb52f5c7ca5da872030b88
Reviewed-on: https://go-review.googlesource.com/8130
Run-TryBot: Austin Clements <austin@google.com>
Reviewed-by: Minux Ma <minux@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-03-26 13:50:22 -06:00
|
|
|
//go:nowritebarrier
|
2014-11-13 19:01:12 -07:00
|
|
|
func newosproc(mp *m, stk unsafe.Pointer) {
|
|
|
|
if false {
|
2015-11-12 15:26:19 -07:00
|
|
|
print("newosproc stk=", stk, " m=", mp, " g=", mp.g0, " id=", mp.id, " ostk=", &mp, "\n")
|
2014-11-13 19:01:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
param := tforkt{
|
|
|
|
tf_tcb: unsafe.Pointer(&mp.tls[0]),
|
|
|
|
tf_tid: (*int32)(unsafe.Pointer(&mp.procid)),
|
|
|
|
tf_stack: uintptr(stk),
|
|
|
|
}
|
|
|
|
|
2016-09-23 18:54:51 -06:00
|
|
|
var oset sigset
|
|
|
|
sigprocmask(_SIG_SETMASK, &sigset_all, &oset)
|
2014-11-13 19:01:12 -07:00
|
|
|
ret := tfork(¶m, unsafe.Sizeof(param), mp, mp.g0, funcPC(mstart))
|
2016-09-23 18:54:51 -06:00
|
|
|
sigprocmask(_SIG_SETMASK, &oset, nil)
|
2014-11-13 19:01:12 -07:00
|
|
|
|
|
|
|
if ret < 0 {
|
|
|
|
print("runtime: failed to create new OS thread (have ", mcount()-1, " already; errno=", -ret, ")\n")
|
2016-06-28 18:06:59 -06:00
|
|
|
if ret == -_EAGAIN {
|
|
|
|
println("runtime: may need to increase max user processes (ulimit -p)")
|
|
|
|
}
|
2014-12-27 21:58:00 -07:00
|
|
|
throw("runtime.newosproc")
|
2014-11-13 19:01:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func osinit() {
|
|
|
|
ncpu = getncpu()
|
2016-07-18 19:40:02 -06:00
|
|
|
physPageSize = getPageSize()
|
2014-11-13 19:01:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var urandom_dev = []byte("/dev/urandom\x00")
|
|
|
|
|
|
|
|
//go:nosplit
|
2014-12-09 15:40:40 -07:00
|
|
|
func getRandomData(r []byte) {
|
2014-11-13 19:01:12 -07:00
|
|
|
fd := open(&urandom_dev[0], 0 /* O_RDONLY */, 0)
|
2014-12-09 15:40:40 -07:00
|
|
|
n := read(fd, unsafe.Pointer(&r[0]), int32(len(r)))
|
2015-04-13 17:37:04 -06:00
|
|
|
closefd(fd)
|
2014-12-09 15:40:40 -07:00
|
|
|
extendRandom(r, int(n))
|
2014-11-13 19:01:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func goenvs() {
|
|
|
|
goenvs_unix()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called to initialize a new m (including the bootstrap m).
|
|
|
|
// Called on the parent thread (main thread in case of bootstrap), can allocate memory.
|
|
|
|
func mpreinit(mp *m) {
|
|
|
|
mp.gsignal = malg(32 * 1024)
|
|
|
|
mp.gsignal.m = mp
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called to initialize a new m (including the bootstrap m).
|
|
|
|
// Called on the new thread, can not allocate memory.
|
|
|
|
func minit() {
|
|
|
|
// m.procid is a uint64, but tfork writes an int32. Fix it up.
|
2016-09-26 12:14:41 -06:00
|
|
|
_g_ := getg()
|
2014-11-13 19:01:12 -07:00
|
|
|
_g_.m.procid = uint64(*(*int32)(unsafe.Pointer(&_g_.m.procid)))
|
|
|
|
|
2016-09-26 12:14:41 -06:00
|
|
|
minitSignals()
|
2014-11-13 19:01:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called from dropm to undo the effect of an minit.
|
2015-11-13 14:21:01 -07:00
|
|
|
//go:nosplit
|
2014-11-13 19:01:12 -07:00
|
|
|
func unminit() {
|
2016-09-26 12:35:55 -06:00
|
|
|
unminitSignals()
|
2014-11-13 19:01:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func memlimit() uintptr {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func sigtramp()
|
|
|
|
|
|
|
|
type sigactiont struct {
|
|
|
|
sa_sigaction uintptr
|
|
|
|
sa_mask uint32
|
|
|
|
sa_flags int32
|
|
|
|
}
|
|
|
|
|
2015-12-26 10:51:59 -07:00
|
|
|
//go:nosplit
|
|
|
|
//go:nowritebarrierrec
|
2014-11-13 19:01:12 -07:00
|
|
|
func setsig(i int32, fn uintptr, restart bool) {
|
|
|
|
var sa sigactiont
|
|
|
|
sa.sa_flags = _SA_SIGINFO | _SA_ONSTACK
|
|
|
|
if restart {
|
|
|
|
sa.sa_flags |= _SA_RESTART
|
|
|
|
}
|
runtime: use a proper type, sigset, for m.sigmask
Replace the cross platform but unsafe [4]uintptr type with a OS
specific type, sigset. Most OSes already define sigset, and this
change defines a suitable sigset for the OSes that don't (darwin,
openbsd). The OSes that don't use m.sigmask (windows, plan9, nacl)
now defines sigset as the empty type, struct{}.
The gain is strongly typed access to m.sigmask, saving a dynamic
size sanity check and unsafe.Pointer casting. Also, some storage is
saved for each M, since [4]uinptr was conservative for most OSes.
The cost is that OSes that don't need m.sigmask has to define sigset.
completes ./all.bash with GOOS linux, on amd64
completes ./make.bash with GOOSes openbsd, android, plan9, windows,
darwin, solaris, netbsd, freebsd, dragonfly, all amd64.
With GOOS=nacl ./make.bash failed with a seemingly unrelated error.
[Replay of CL 16942 by Elias Naur.]
Change-Id: I98f144d626033ae5318576115ed635415ac71b2c
Reviewed-on: https://go-review.googlesource.com/17033
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
2015-11-17 03:41:06 -07:00
|
|
|
sa.sa_mask = uint32(sigset_all)
|
2014-11-13 19:01:12 -07:00
|
|
|
if fn == funcPC(sighandler) {
|
|
|
|
fn = funcPC(sigtramp)
|
|
|
|
}
|
|
|
|
sa.sa_sigaction = fn
|
|
|
|
sigaction(i, &sa, nil)
|
|
|
|
}
|
|
|
|
|
2015-12-26 10:51:59 -07:00
|
|
|
//go:nosplit
|
|
|
|
//go:nowritebarrierrec
|
2014-12-19 14:16:17 -07:00
|
|
|
func setsigstack(i int32) {
|
2014-12-27 21:58:00 -07:00
|
|
|
throw("setsigstack")
|
2014-12-19 14:16:17 -07:00
|
|
|
}
|
|
|
|
|
2015-12-26 10:51:59 -07:00
|
|
|
//go:nosplit
|
|
|
|
//go:nowritebarrierrec
|
2014-11-13 19:01:12 -07:00
|
|
|
func getsig(i int32) uintptr {
|
|
|
|
var sa sigactiont
|
|
|
|
sigaction(i, nil, &sa)
|
|
|
|
if sa.sa_sigaction == funcPC(sigtramp) {
|
|
|
|
return funcPC(sighandler)
|
|
|
|
}
|
|
|
|
return sa.sa_sigaction
|
|
|
|
}
|
|
|
|
|
2016-09-25 14:38:54 -06:00
|
|
|
// setSignaltstackSP sets the ss_sp field of a stackt.
|
2015-11-13 14:21:01 -07:00
|
|
|
//go:nosplit
|
2016-09-25 14:38:54 -06:00
|
|
|
func setSignalstackSP(s *stackt, sp uintptr) {
|
|
|
|
s.ss_sp = sp
|
2014-11-13 19:01:12 -07:00
|
|
|
}
|
|
|
|
|
2015-12-26 10:51:59 -07:00
|
|
|
//go:nosplit
|
|
|
|
//go:nowritebarrierrec
|
2016-09-23 18:54:51 -06:00
|
|
|
func sigmaskToSigset(m sigmask) sigset {
|
|
|
|
return sigset(m[0])
|
2015-07-21 23:34:48 -06:00
|
|
|
}
|
2016-09-25 22:33:27 -06:00
|
|
|
|
2016-09-26 12:14:41 -06:00
|
|
|
func sigdelset(mask *sigset, i int) {
|
|
|
|
*mask &^= 1 << (uint32(i) - 1)
|
|
|
|
}
|
|
|
|
|
2016-09-25 22:33:27 -06:00
|
|
|
func (c *sigctxt) fixsigcode(sig uint32) {
|
|
|
|
}
|