2014-11-11 21:00:29 -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-11 10:39:30 -07:00
|
|
|
import (
|
|
|
|
"runtime/internal/sys"
|
|
|
|
"unsafe"
|
|
|
|
)
|
2014-11-11 21:00:29 -07:00
|
|
|
|
2016-05-06 09:26:37 -06:00
|
|
|
type mOS struct{}
|
|
|
|
|
|
|
|
//go:noescape
|
2018-02-13 20:00:17 -07:00
|
|
|
func thr_new(param *thrparam, size int32) int32
|
2016-05-06 09:26:37 -06:00
|
|
|
|
|
|
|
//go:noescape
|
|
|
|
func sigaltstack(new, old *stackt)
|
|
|
|
|
|
|
|
//go:noescape
|
|
|
|
func sigprocmask(how int32, new, old *sigset)
|
|
|
|
|
|
|
|
//go:noescape
|
|
|
|
func setitimer(mode int32, new, old *itimerval)
|
|
|
|
|
|
|
|
//go:noescape
|
|
|
|
func sysctl(mib *uint32, miblen uint32, out *byte, size *uintptr, dst *byte, ndst uintptr) int32
|
|
|
|
|
runtime: minor simplifications to signal code
Change setsig, setsigstack, getsig, raise, raiseproc to take uint32 for
signal number parameter, as that is the type mostly used for signal
numbers. Same for dieFromSignal, sigInstallGoHandler, raisebadsignal.
Remove setsig restart parameter, as it is always either true or
irrelevant.
Don't check the handler in setsigstack, as the only caller does that
anyhow.
Don't bother to convert the handler from sigtramp to sighandler in
getsig, as it will never be called when the handler is sigtramp or
sighandler.
Don't check the return value from rt_sigaction in the GNU/Linux version
of setsigstack; no other setsigstack checks it, and it never fails.
Change-Id: I6bbd677e048a77eddf974dd3d017bc3c560fbd48
Reviewed-on: https://go-review.googlesource.com/29953
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2016-09-27 23:24:51 -06:00
|
|
|
func raise(sig uint32)
|
|
|
|
func raiseproc(sig uint32)
|
2016-05-06 09:26:37 -06:00
|
|
|
|
|
|
|
//go:noescape
|
2016-09-28 15:01:27 -06:00
|
|
|
func sys_umtx_op(addr *uint32, mode int32, val uint32, uaddr1 uintptr, ut *umtx_time) int32
|
2016-05-06 09:26:37 -06:00
|
|
|
|
|
|
|
func osyield()
|
|
|
|
|
2014-11-11 21:00:29 -07:00
|
|
|
// From FreeBSD's <sys/sysctl.h>
|
|
|
|
const (
|
2016-07-18 19:40:02 -06:00
|
|
|
_CTL_HW = 6
|
|
|
|
_HW_PAGESIZE = 7
|
2014-11-11 21:00:29 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var sigset_all = sigset{[4]uint32{^uint32(0), ^uint32(0), ^uint32(0), ^uint32(0)}}
|
|
|
|
|
2017-03-10 13:13:20 -07:00
|
|
|
// Undocumented numbers from FreeBSD's lib/libc/gen/sysctlnametomib.c.
|
|
|
|
const (
|
|
|
|
_CTL_QUERY = 0
|
|
|
|
_CTL_QUERY_MIB = 3
|
|
|
|
)
|
|
|
|
|
|
|
|
// sysctlnametomib fill mib with dynamically assigned sysctl entries of name,
|
|
|
|
// return count of effected mib slots, return 0 on error.
|
|
|
|
func sysctlnametomib(name []byte, mib *[_CTL_MAXNAME]uint32) uint32 {
|
|
|
|
oid := [2]uint32{_CTL_QUERY, _CTL_QUERY_MIB}
|
|
|
|
miblen := uintptr(_CTL_MAXNAME)
|
|
|
|
if sysctl(&oid[0], 2, (*byte)(unsafe.Pointer(mib)), &miblen, (*byte)(unsafe.Pointer(&name[0])), (uintptr)(len(name))) < 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
miblen /= unsafe.Sizeof(uint32(0))
|
|
|
|
if miblen <= 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return uint32(miblen)
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
_CPU_CURRENT_PID = -1 // Current process ID.
|
|
|
|
)
|
|
|
|
|
|
|
|
//go:noescape
|
|
|
|
func cpuset_getaffinity(level int, which int, id int64, size int, mask *byte) int32
|
|
|
|
|
2017-09-26 09:18:37 -06:00
|
|
|
//go:systemstack
|
2014-11-11 21:00:29 -07:00
|
|
|
func getncpu() int32 {
|
2017-09-26 09:18:37 -06:00
|
|
|
// Use a large buffer for the CPU mask. We're on the system
|
|
|
|
// stack, so this is fine, and we can't allocate memory for a
|
|
|
|
// dynamically-sized buffer at this point.
|
|
|
|
const maxCPUs = 64 * 1024
|
|
|
|
var mask [maxCPUs / 8]byte
|
2017-03-10 13:13:20 -07:00
|
|
|
var mib [_CTL_MAXNAME]uint32
|
|
|
|
|
|
|
|
// According to FreeBSD's /usr/src/sys/kern/kern_cpuset.c,
|
|
|
|
// cpuset_getaffinity return ERANGE when provided buffer size exceed the limits in kernel.
|
|
|
|
// Querying kern.smp.maxcpus to calculate maximum buffer size.
|
|
|
|
// See https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=200802
|
|
|
|
|
|
|
|
// Variable kern.smp.maxcpus introduced at Dec 23 2003, revision 123766,
|
|
|
|
// with dynamically assigned sysctl entries.
|
|
|
|
miblen := sysctlnametomib([]byte("kern.smp.maxcpus"), &mib)
|
|
|
|
if miblen == 0 {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query kern.smp.maxcpus.
|
|
|
|
dstsize := uintptr(4)
|
|
|
|
maxcpus := uint32(0)
|
|
|
|
if sysctl(&mib[0], miblen, (*byte)(unsafe.Pointer(&maxcpus)), &dstsize, nil, 0) != 0 {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-09-26 09:18:37 -06:00
|
|
|
maskSize := int(maxcpus+7) / 8
|
|
|
|
if maskSize < sys.PtrSize {
|
|
|
|
maskSize = sys.PtrSize
|
2017-03-10 13:13:20 -07:00
|
|
|
}
|
2017-09-26 09:18:37 -06:00
|
|
|
if maskSize > len(mask) {
|
|
|
|
maskSize = len(mask)
|
2017-03-10 13:13:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if cpuset_getaffinity(_CPU_LEVEL_WHICH, _CPU_WHICH_PID, _CPU_CURRENT_PID,
|
2017-09-26 09:18:37 -06:00
|
|
|
maskSize, (*byte)(unsafe.Pointer(&mask[0]))) != 0 {
|
2017-03-10 13:13:20 -07:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
n := int32(0)
|
2017-09-26 09:18:37 -06:00
|
|
|
for _, v := range mask[:maskSize] {
|
2017-03-10 13:13:20 -07:00
|
|
|
for v != 0 {
|
|
|
|
n += int32(v & 1)
|
|
|
|
v >>= 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if n == 0 {
|
|
|
|
return 1
|
2014-11-11 21:00:29 -07:00
|
|
|
}
|
2017-03-10 13:13:20 -07:00
|
|
|
return n
|
2014-11-11 21:00:29 -07:00
|
|
|
}
|
|
|
|
|
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-11 21:00:29 -07:00
|
|
|
// FreeBSD's umtx_op syscall is effectively the same as Linux's futex, and
|
|
|
|
// thus the code is largely similar. See Linux implementation
|
2015-03-11 13:58:47 -06:00
|
|
|
// and lock_futex.go for comments.
|
2014-11-11 21:00:29 -07:00
|
|
|
|
|
|
|
//go:nosplit
|
|
|
|
func futexsleep(addr *uint32, val uint32, ns int64) {
|
[dev.cc] runtime: delete scalararg, ptrarg; rename onM to systemstack
Scalararg and ptrarg are not "signal safe".
Go code filling them out can be interrupted by a signal,
and then the signal handler runs, and if it also ends up
in Go code that uses scalararg or ptrarg, now the old
values have been smashed.
For the pieces of code that do need to run in a signal handler,
we introduced onM_signalok, which is really just onM
except that the _signalok is meant to convey that the caller
asserts that scalarg and ptrarg will be restored to their old
values after the call (instead of the usual behavior, zeroing them).
Scalararg and ptrarg are also untyped and therefore error-prone.
Go code can always pass a closure instead of using scalararg
and ptrarg; they were only really necessary for C code.
And there's no more C code.
For all these reasons, delete scalararg and ptrarg, converting
the few remaining references to use closures.
Once those are gone, there is no need for a distinction between
onM and onM_signalok, so replace both with a single function
equivalent to the current onM_signalok (that is, it can be called
on any of the curg, g0, and gsignal stacks).
The name onM and the phrase 'm stack' are misnomers,
because on most system an M has two system stacks:
the main thread stack and the signal handling stack.
Correct the misnomer by naming the replacement function systemstack.
Fix a few references to "M stack" in code.
The main motivation for this change is to eliminate scalararg/ptrarg.
Rick and I have already seen them cause problems because
the calling sequence m.ptrarg[0] = p is a heap pointer assignment,
so it gets a write barrier. The write barrier also uses onM, so it has
all the same problems as if it were being invoked by a signal handler.
We worked around this by saving and restoring the old values
and by calling onM_signalok, but there's no point in keeping this nice
home for bugs around any longer.
This CL also changes funcline to return the file name as a result
instead of filling in a passed-in *string. (The *string signature is
left over from when the code was written in and called from C.)
That's arguably an unrelated change, except that once I had done
the ptrarg/scalararg/onM cleanup I started getting false positives
about the *string argument escaping (not allowed in package runtime).
The compiler is wrong, but the easiest fix is to write the code like
Go code instead of like C code. I am a bit worried that the compiler
is wrong because of some use of uninitialized memory in the escape
analysis. If that's the reason, it will go away when we convert the
compiler to Go. (And if not, we'll debug it the next time.)
LGTM=khr
R=r, khr
CC=austin, golang-codereviews, iant, rlh
https://golang.org/cl/174950043
2014-11-12 12:54:31 -07:00
|
|
|
systemstack(func() {
|
2014-11-11 21:00:29 -07:00
|
|
|
futexsleep1(addr, val, ns)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func futexsleep1(addr *uint32, val uint32, ns int64) {
|
2016-09-28 15:01:27 -06:00
|
|
|
var utp *umtx_time
|
2014-11-11 21:00:29 -07:00
|
|
|
if ns >= 0 {
|
2016-09-28 15:01:27 -06:00
|
|
|
var ut umtx_time
|
|
|
|
ut._clockid = _CLOCK_MONOTONIC
|
|
|
|
ut._timeout.set_sec(int64(timediv(ns, 1000000000, (*int32)(unsafe.Pointer(&ut._timeout.tv_nsec)))))
|
|
|
|
utp = &ut
|
2014-11-11 21:00:29 -07:00
|
|
|
}
|
2016-09-28 15:01:27 -06:00
|
|
|
ret := sys_umtx_op(addr, _UMTX_OP_WAIT_UINT_PRIVATE, val, unsafe.Sizeof(*utp), utp)
|
2014-11-11 21:00:29 -07:00
|
|
|
if ret >= 0 || ret == -_EINTR {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
print("umtx_wait addr=", addr, " val=", val, " ret=", ret, "\n")
|
|
|
|
*(*int32)(unsafe.Pointer(uintptr(0x1005))) = 0x1005
|
|
|
|
}
|
|
|
|
|
|
|
|
//go:nosplit
|
|
|
|
func futexwakeup(addr *uint32, cnt uint32) {
|
2016-09-28 15:01:27 -06:00
|
|
|
ret := sys_umtx_op(addr, _UMTX_OP_WAKE_PRIVATE, cnt, 0, nil)
|
2014-11-11 21:00:29 -07:00
|
|
|
if ret >= 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
[dev.cc] runtime: delete scalararg, ptrarg; rename onM to systemstack
Scalararg and ptrarg are not "signal safe".
Go code filling them out can be interrupted by a signal,
and then the signal handler runs, and if it also ends up
in Go code that uses scalararg or ptrarg, now the old
values have been smashed.
For the pieces of code that do need to run in a signal handler,
we introduced onM_signalok, which is really just onM
except that the _signalok is meant to convey that the caller
asserts that scalarg and ptrarg will be restored to their old
values after the call (instead of the usual behavior, zeroing them).
Scalararg and ptrarg are also untyped and therefore error-prone.
Go code can always pass a closure instead of using scalararg
and ptrarg; they were only really necessary for C code.
And there's no more C code.
For all these reasons, delete scalararg and ptrarg, converting
the few remaining references to use closures.
Once those are gone, there is no need for a distinction between
onM and onM_signalok, so replace both with a single function
equivalent to the current onM_signalok (that is, it can be called
on any of the curg, g0, and gsignal stacks).
The name onM and the phrase 'm stack' are misnomers,
because on most system an M has two system stacks:
the main thread stack and the signal handling stack.
Correct the misnomer by naming the replacement function systemstack.
Fix a few references to "M stack" in code.
The main motivation for this change is to eliminate scalararg/ptrarg.
Rick and I have already seen them cause problems because
the calling sequence m.ptrarg[0] = p is a heap pointer assignment,
so it gets a write barrier. The write barrier also uses onM, so it has
all the same problems as if it were being invoked by a signal handler.
We worked around this by saving and restoring the old values
and by calling onM_signalok, but there's no point in keeping this nice
home for bugs around any longer.
This CL also changes funcline to return the file name as a result
instead of filling in a passed-in *string. (The *string signature is
left over from when the code was written in and called from C.)
That's arguably an unrelated change, except that once I had done
the ptrarg/scalararg/onM cleanup I started getting false positives
about the *string argument escaping (not allowed in package runtime).
The compiler is wrong, but the easiest fix is to write the code like
Go code instead of like C code. I am a bit worried that the compiler
is wrong because of some use of uninitialized memory in the escape
analysis. If that's the reason, it will go away when we convert the
compiler to Go. (And if not, we'll debug it the next time.)
LGTM=khr
R=r, khr
CC=austin, golang-codereviews, iant, rlh
https://golang.org/cl/174950043
2014-11-12 12:54:31 -07:00
|
|
|
systemstack(func() {
|
2014-11-11 21:00:29 -07:00
|
|
|
print("umtx_wake_addr=", addr, " ret=", ret, "\n")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func thr_start()
|
|
|
|
|
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-11 21:00:29 -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, " thr_start=", funcPC(thr_start), " id=", mp.id, " ostk=", &mp, "\n")
|
2014-11-11 21:00:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
param := thrparam{
|
|
|
|
start_func: funcPC(thr_start),
|
|
|
|
arg: unsafe.Pointer(mp),
|
2018-02-13 20:00:17 -07:00
|
|
|
stack_base: mp.g0.stack.lo,
|
|
|
|
stack_size: uintptr(stk) - mp.g0.stack.lo,
|
2014-11-11 21:00:29 -07:00
|
|
|
child_tid: unsafe.Pointer(&mp.procid),
|
|
|
|
parent_tid: nil,
|
|
|
|
tls_base: unsafe.Pointer(&mp.tls[0]),
|
|
|
|
tls_size: unsafe.Sizeof(mp.tls),
|
|
|
|
}
|
|
|
|
|
|
|
|
var oset sigset
|
2015-07-21 23:34:48 -06:00
|
|
|
sigprocmask(_SIG_SETMASK, &sigset_all, &oset)
|
2016-06-28 18:06:59 -06:00
|
|
|
// TODO: Check for error.
|
2018-02-13 20:00:17 -07:00
|
|
|
ret := thr_new(¶m, int32(unsafe.Sizeof(param)))
|
|
|
|
sigprocmask(_SIG_SETMASK, &oset, nil)
|
|
|
|
if ret < 0 {
|
|
|
|
print("runtime: failed to create new OS thread (have ", mcount(), " already; errno=", -ret, ")\n")
|
|
|
|
throw("newosproc")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Version of newosproc that doesn't require a valid G.
|
|
|
|
//go:nosplit
|
|
|
|
func newosproc0(stacksize uintptr, fn unsafe.Pointer) {
|
|
|
|
stack := sysAlloc(stacksize, &memstats.stacks_sys)
|
|
|
|
if stack == nil {
|
|
|
|
write(2, unsafe.Pointer(&failallocatestack[0]), int32(len(failallocatestack)))
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
// This code "knows" it's being called once from the library
|
|
|
|
// initialization code, and so it's using the static m0 for the
|
|
|
|
// tls and procid (thread) pointers. thr_new() requires the tls
|
|
|
|
// pointers, though the tid pointers can be nil.
|
|
|
|
// However, newosproc0 is currently unreachable because builds
|
|
|
|
// utilizing c-shared/c-archive force external linking.
|
|
|
|
param := thrparam{
|
|
|
|
start_func: funcPC(fn),
|
|
|
|
arg: nil,
|
|
|
|
stack_base: uintptr(stack), //+stacksize?
|
|
|
|
stack_size: stacksize,
|
|
|
|
child_tid: unsafe.Pointer(&m0.procid),
|
|
|
|
parent_tid: nil,
|
|
|
|
tls_base: unsafe.Pointer(&m0.tls[0]),
|
|
|
|
tls_size: unsafe.Sizeof(m0.tls),
|
|
|
|
}
|
|
|
|
|
|
|
|
var oset sigset
|
|
|
|
sigprocmask(_SIG_SETMASK, &sigset_all, &oset)
|
|
|
|
ret := thr_new(¶m, int32(unsafe.Sizeof(param)))
|
2015-07-21 23:34:48 -06:00
|
|
|
sigprocmask(_SIG_SETMASK, &oset, nil)
|
2018-02-13 20:00:17 -07:00
|
|
|
if ret < 0 {
|
|
|
|
write(2, unsafe.Pointer(&failthreadcreate[0]), int32(len(failthreadcreate)))
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var failallocatestack = []byte("runtime: failed to allocate stack for the new OS thread\n")
|
|
|
|
var failthreadcreate = []byte("runtime: failed to create new OS thread\n")
|
|
|
|
|
|
|
|
// Called to do synchronous initialization of Go code built with
|
|
|
|
// -buildmode=c-archive or -buildmode=c-shared.
|
|
|
|
// None of the Go runtime is initialized.
|
|
|
|
//go:nosplit
|
|
|
|
//go:nowritebarrierrec
|
|
|
|
func libpreinit() {
|
|
|
|
initsig(true)
|
2014-11-11 21:00:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func osinit() {
|
|
|
|
ncpu = getncpu()
|
2018-03-09 03:44:59 -07:00
|
|
|
if physPageSize == 0 {
|
|
|
|
physPageSize = getPageSize()
|
|
|
|
}
|
2014-11-11 21:00:29 -07:00
|
|
|
}
|
|
|
|
|
2015-01-08 16:30:22 -07:00
|
|
|
var urandom_dev = []byte("/dev/urandom\x00")
|
2014-11-11 21:00:29 -07:00
|
|
|
|
|
|
|
//go:nosplit
|
2014-12-09 15:40:40 -07:00
|
|
|
func getRandomData(r []byte) {
|
2014-11-11 21:00:29 -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-11 21:00:29 -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).
|
2016-01-27 13:49:13 -07:00
|
|
|
// Called on the new thread, cannot allocate memory.
|
2014-11-11 21:00:29 -07:00
|
|
|
func minit() {
|
|
|
|
// m.procid is a uint64, but thr_new writes a uint32 on 32-bit systems.
|
|
|
|
// Fix it up. (Only matters on big-endian, but be clean anyway.)
|
2015-11-11 10:39:30 -07:00
|
|
|
if sys.PtrSize == 4 {
|
2016-09-26 12:14:41 -06:00
|
|
|
_g_ := getg()
|
2014-11-11 21:00:29 -07:00
|
|
|
_g_.m.procid = uint64(*(*uint32)(unsafe.Pointer(&_g_.m.procid)))
|
|
|
|
}
|
|
|
|
|
2017-03-17 14:48:56 -06:00
|
|
|
// On FreeBSD before about April 2017 there was a bug such
|
|
|
|
// that calling execve from a thread other than the main
|
|
|
|
// thread did not reset the signal stack. That would confuse
|
|
|
|
// minitSignals, which calls minitSignalStack, which checks
|
|
|
|
// whether there is currently a signal stack and uses it if
|
|
|
|
// present. To avoid this confusion, explicitly disable the
|
|
|
|
// signal stack on the main thread when not running in a
|
|
|
|
// library. This can be removed when we are confident that all
|
|
|
|
// FreeBSD users are running a patched kernel. See issue #15658.
|
|
|
|
if gp := getg(); !isarchive && !islibrary && gp.m == &m0 && gp == gp.m.g0 {
|
|
|
|
st := stackt{ss_flags: _SS_DISABLE}
|
|
|
|
sigaltstack(&st, nil)
|
|
|
|
}
|
|
|
|
|
2016-09-26 12:14:41 -06:00
|
|
|
minitSignals()
|
2014-11-11 21:00:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called from dropm to undo the effect of an minit.
|
2015-11-13 14:21:01 -07:00
|
|
|
//go:nosplit
|
2014-11-11 21:00:29 -07:00
|
|
|
func unminit() {
|
2016-09-26 12:35:55 -06:00
|
|
|
unminitSignals()
|
2014-11-11 21:00:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func sigtramp()
|
|
|
|
|
|
|
|
type sigactiont struct {
|
|
|
|
sa_handler uintptr
|
|
|
|
sa_flags int32
|
|
|
|
sa_mask sigset
|
|
|
|
}
|
|
|
|
|
2018-02-13 20:00:17 -07:00
|
|
|
// See os_freebsd2.go, os_freebsd_amd64.go for setsig function
|
|
|
|
|
2015-12-26 10:51:59 -07:00
|
|
|
//go:nosplit
|
|
|
|
//go:nowritebarrierrec
|
2018-02-13 20:00:17 -07:00
|
|
|
func setsigstack(i uint32) {
|
2014-11-11 21:00:29 -07:00
|
|
|
var sa sigactiont
|
2018-02-13 20:00:17 -07:00
|
|
|
sigaction(i, nil, &sa)
|
|
|
|
if sa.sa_flags&_SA_ONSTACK != 0 {
|
|
|
|
return
|
2014-11-11 21:00:29 -07:00
|
|
|
}
|
2018-02-13 20:00:17 -07:00
|
|
|
sa.sa_flags |= _SA_ONSTACK
|
2014-11-11 21:00:29 -07:00
|
|
|
sigaction(i, &sa, nil)
|
|
|
|
}
|
2014-12-19 14:16:17 -07:00
|
|
|
|
2015-12-26 10:51:59 -07:00
|
|
|
//go:nosplit
|
|
|
|
//go:nowritebarrierrec
|
runtime: minor simplifications to signal code
Change setsig, setsigstack, getsig, raise, raiseproc to take uint32 for
signal number parameter, as that is the type mostly used for signal
numbers. Same for dieFromSignal, sigInstallGoHandler, raisebadsignal.
Remove setsig restart parameter, as it is always either true or
irrelevant.
Don't check the handler in setsigstack, as the only caller does that
anyhow.
Don't bother to convert the handler from sigtramp to sighandler in
getsig, as it will never be called when the handler is sigtramp or
sighandler.
Don't check the return value from rt_sigaction in the GNU/Linux version
of setsigstack; no other setsigstack checks it, and it never fails.
Change-Id: I6bbd677e048a77eddf974dd3d017bc3c560fbd48
Reviewed-on: https://go-review.googlesource.com/29953
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2016-09-27 23:24:51 -06:00
|
|
|
func getsig(i uint32) uintptr {
|
2014-11-11 21:00:29 -07:00
|
|
|
var sa sigactiont
|
|
|
|
sigaction(i, nil, &sa)
|
|
|
|
return sa.sa_handler
|
|
|
|
}
|
|
|
|
|
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-11 21:00:29 -07:00
|
|
|
}
|
|
|
|
|
2015-12-26 10:51:59 -07:00
|
|
|
//go:nosplit
|
|
|
|
//go:nowritebarrierrec
|
2016-09-27 14:42:28 -06:00
|
|
|
func sigaddset(mask *sigset, i int) {
|
|
|
|
mask.__bits[(i-1)/32] |= 1 << ((uint32(i) - 1) & 31)
|
2014-11-11 21:00:29 -07:00
|
|
|
}
|
2016-09-25 22:33:27 -06:00
|
|
|
|
2016-09-26 12:14:41 -06:00
|
|
|
func sigdelset(mask *sigset, i int) {
|
|
|
|
mask.__bits[(i-1)/32] &^= 1 << ((uint32(i) - 1) & 31)
|
|
|
|
}
|
|
|
|
|
2016-09-25 22:33:27 -06:00
|
|
|
func (c *sigctxt) fixsigcode(sig uint32) {
|
|
|
|
}
|
2018-03-09 03:44:59 -07:00
|
|
|
|
|
|
|
func sysargs(argc int32, argv **byte) {
|
|
|
|
n := argc + 1
|
|
|
|
|
|
|
|
// skip over argv, envp to get to auxv
|
|
|
|
for argv_index(argv, n) != nil {
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip NULL separator
|
|
|
|
n++
|
|
|
|
|
|
|
|
// now argv+n is auxv
|
|
|
|
auxv := (*[1 << 28]uintptr)(add(unsafe.Pointer(argv), uintptr(n)*sys.PtrSize))
|
|
|
|
sysauxv(auxv[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
_AT_NULL = 0 // Terminates the vector
|
|
|
|
_AT_PAGESZ = 6 // Page size in bytes
|
2018-03-23 08:53:26 -06:00
|
|
|
_AT_HWCAP = 26 // CPU feature flags
|
2018-03-09 03:44:59 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func sysauxv(auxv []uintptr) {
|
|
|
|
for i := 0; auxv[i] != _AT_NULL; i += 2 {
|
|
|
|
tag, val := auxv[i], auxv[i+1]
|
|
|
|
switch tag {
|
|
|
|
// _AT_NCPUS from auxv shouldn't be used due to golang.org/issue/15206
|
|
|
|
case _AT_PAGESZ:
|
|
|
|
physPageSize = val
|
|
|
|
}
|
|
|
|
|
|
|
|
archauxv(tag, val)
|
|
|
|
}
|
|
|
|
}
|
2018-02-13 20:00:17 -07:00
|
|
|
|
|
|
|
// sysSigaction calls the sigaction system call.
|
|
|
|
//go:nosplit
|
|
|
|
func sysSigaction(sig uint32, new, old *sigactiont) {
|
|
|
|
// Use system stack to avoid split stack overflow on amd64
|
|
|
|
if asmSigaction(uintptr(sig), new, old) != 0 {
|
|
|
|
systemstack(func() {
|
|
|
|
throw("sigaction failed")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// asmSigaction is implemented in assembly.
|
|
|
|
//go:noescape
|
|
|
|
func asmSigaction(sig uintptr, new, old *sigactiont) int32
|