Skip to content

Commit

Permalink
runtime: M-targeted signals for libc-based OSes
Browse files Browse the repository at this point in the history
For #10958, #24543.

Change-Id: I82bee63b49e15bd5a53228eb85179814c80437ef
Reviewed-on: https://go-review.googlesource.com/c/go/+/201403
Run-TryBot: Austin Clements <austin@google.com>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
  • Loading branch information
aclements committed Oct 26, 2019
1 parent 8714e39 commit 42aab4b
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/runtime/os2_aix.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ var (
//go:cgo_import_dynamic libpthread_attr_setstackaddr pthread_attr_setstackaddr "libpthread.a/shr_xpg5_64.o"
//go:cgo_import_dynamic libpthread_create pthread_create "libpthread.a/shr_xpg5_64.o"
//go:cgo_import_dynamic libpthread_sigthreadmask sigthreadmask "libpthread.a/shr_xpg5_64.o"
//go:cgo_import_dynamic libpthread_self pthread_self "libpthread.a/shr_xpg5_64.o"
//go:cgo_import_dynamic libpthread_kill pthread_kill "libpthread.a/shr_xpg5_64.o"

//go:linkname libc__Errno libc__Errno
//go:linkname libc_clock_gettime libc_clock_gettime
Expand Down Expand Up @@ -101,6 +103,8 @@ var (
//go:linkname libpthread_attr_setstackaddr libpthread_attr_setstackaddr
//go:linkname libpthread_create libpthread_create
//go:linkname libpthread_sigthreadmask libpthread_sigthreadmask
//go:linkname libpthread_self libpthread_self
//go:linkname libpthread_kill libpthread_kill

var (
//libc
Expand Down Expand Up @@ -139,7 +143,9 @@ var (
libpthread_attr_setdetachstate,
libpthread_attr_setstackaddr,
libpthread_create,
libpthread_sigthreadmask libFunc
libpthread_sigthreadmask,
libpthread_self,
libpthread_kill libFunc
)

type libFunc uintptr
Expand Down Expand Up @@ -724,3 +730,14 @@ func sigprocmask(how int32, new, old *sigset) {
sigprocmask1(uintptr(how), uintptr(unsafe.Pointer(new)), uintptr(unsafe.Pointer(old)))

}

//go:nosplit
func pthread_self() pthread {
r, _ := syscall0(&libpthread_self)
return pthread(r)
}

//go:nosplit
func signalM(mp *m, sig int) {
syscall2(&libpthread_kill, uintptr(pthread(mp.procid)), uintptr(sig))
}
16 changes: 16 additions & 0 deletions src/runtime/os3_solaris.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
//go:cgo_import_dynamic libc_pthread_attr_setdetachstate pthread_attr_setdetachstate "libc.so"
//go:cgo_import_dynamic libc_pthread_attr_setstack pthread_attr_setstack "libc.so"
//go:cgo_import_dynamic libc_pthread_create pthread_create "libc.so"
//go:cgo_import_dynamic libc_pthread_self pthread_self "libc.so"
//go:cgo_import_dynamic libc_pthread_kill pthread_kill "libc.so"
//go:cgo_import_dynamic libc_raise raise "libc.so"
//go:cgo_import_dynamic libc_read read "libc.so"
//go:cgo_import_dynamic libc_select select "libc.so"
Expand Down Expand Up @@ -61,6 +63,8 @@ import (
//go:linkname libc_pthread_attr_setdetachstate libc_pthread_attr_setdetachstate
//go:linkname libc_pthread_attr_setstack libc_pthread_attr_setstack
//go:linkname libc_pthread_create libc_pthread_create
//go:linkname libc_pthread_self libc_pthread_self
//go:linkname libc_pthread_kill libc_pthread_kill
//go:linkname libc_raise libc_raise
//go:linkname libc_read libc_read
//go:linkname libc_select libc_select
Expand Down Expand Up @@ -94,6 +98,8 @@ var (
libc_pthread_attr_setdetachstate,
libc_pthread_attr_setstack,
libc_pthread_create,
libc_pthread_self,
libc_pthread_kill,
libc_raise,
libc_read,
libc_sched_yield,
Expand Down Expand Up @@ -214,6 +220,8 @@ func minit() {
asmcgocall(unsafe.Pointer(funcPC(miniterrno)), unsafe.Pointer(&libc____errno))

minitSignals()

getg().m.procid = uint64(pthread_self())
}

// Called from dropm to undo the effect of an minit.
Expand Down Expand Up @@ -434,6 +442,14 @@ func pthread_create(thread *pthread, attr *pthreadattr, fn uintptr, arg unsafe.P
return int32(sysvicall4(&libc_pthread_create, uintptr(unsafe.Pointer(thread)), uintptr(unsafe.Pointer(attr)), uintptr(fn), uintptr(arg)))
}

func pthread_self() pthread {
return pthread(sysvicall0(&libc_pthread_self))
}

func signalM(mp *m, sig int) {
sysvicall2(&libc_pthread_kill, uintptr(pthread(mp.procid)), uintptr(sig))
}

//go:nosplit
//go:nowritebarrierrec
func raise(sig uint32) /* int32 */ {
Expand Down
1 change: 1 addition & 0 deletions src/runtime/os_aix.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ func miniterrno() {
func minit() {
miniterrno()
minitSignals()
getg().m.procid = uint64(pthread_self())
}

func unminit() {
Expand Down
5 changes: 5 additions & 0 deletions src/runtime/os_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ func minit() {
minitSignalStack()
}
minitSignalMask()
getg().m.procid = uint64(pthread_self())
}

// Called from dropm to undo the effect of an minit.
Expand Down Expand Up @@ -406,3 +407,7 @@ func sysargs(argc int32, argv **byte) {
executablePath = executablePath[len(prefix):]
}
}

func signalM(mp *m, sig int) {
pthread_kill(pthread(mp.procid), uint32(sig))
}
10 changes: 10 additions & 0 deletions src/runtime/sys_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ func pthread_self() (t pthread) {
}
func pthread_self_trampoline()

//go:nosplit
//go:cgo_unsafe_args
func pthread_kill(t pthread, sig uint32) {
libcCall(unsafe.Pointer(funcPC(pthread_kill_trampoline)), unsafe.Pointer(&t))
return
}
func pthread_kill_trampoline()

func mmap(addr unsafe.Pointer, n uintptr, prot, flags, fd int32, off uint32) (unsafe.Pointer, int) {
args := struct {
addr unsafe.Pointer
Expand Down Expand Up @@ -415,6 +423,8 @@ func setNonblock(fd int32) {
//go:cgo_import_dynamic libc_pthread_attr_getstacksize pthread_attr_getstacksize "/usr/lib/libSystem.B.dylib"
//go:cgo_import_dynamic libc_pthread_attr_setdetachstate pthread_attr_setdetachstate "/usr/lib/libSystem.B.dylib"
//go:cgo_import_dynamic libc_pthread_create pthread_create "/usr/lib/libSystem.B.dylib"
//go:cgo_import_dynamic libc_pthread_self pthread_self "/usr/lib/libSystem.B.dylib"
//go:cgo_import_dynamic libc_pthread_kill pthread_kill "/usr/lib/libSystem.B.dylib"
//go:cgo_import_dynamic libc_exit exit "/usr/lib/libSystem.B.dylib"
//go:cgo_import_dynamic libc_raise raise "/usr/lib/libSystem.B.dylib"

Expand Down
25 changes: 25 additions & 0 deletions src/runtime/sys_darwin_386.s
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,31 @@ TEXT runtime·pthread_cond_signal_trampoline(SB),NOSPLIT,$0
POPL BP
RET

TEXT runtime·pthread_self_trampoline(SB),NOSPLIT,$0
PUSHL BP
MOVL SP, BP
NOP SP // hide SP from vet
CALL libc_pthread_self(SB)
MOVL 8(SP), CX
MOVL AX, 0(CX) // return value
MOVL BP, SP
POPL BP
RET

TEXT runtime·pthread_kill_trampoline(SB),NOSPLIT,$0
PUSHL BP
MOVL SP, BP
SUBL $8, SP
MOVL 16(SP), CX
MOVL 0(CX), AX // arg 1 thread
MOVL AX, 0(SP)
MOVL 4(CX), AX // arg 2 sig
MOVL AX, 4(SP)
CALL libc_pthread_kill(SB)
MOVL BP, SP
POPL BP
RET

// syscall calls a function in libc on behalf of the syscall package.
// syscall takes a pointer to a struct like:
// struct {
Expand Down
18 changes: 18 additions & 0 deletions src/runtime/sys_darwin_amd64.s
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,24 @@ TEXT runtime·pthread_cond_signal_trampoline(SB),NOSPLIT,$0
POPQ BP
RET

TEXT runtime·pthread_self_trampoline(SB),NOSPLIT,$0
PUSHQ BP
MOVQ SP, BP
MOVQ DI, BX // BX is caller-save
CALL libc_pthread_self(SB)
MOVQ AX, 0(BX) // return value
POPQ BP
RET

TEXT runtime·pthread_kill_trampoline(SB),NOSPLIT,$0
PUSHQ BP
MOVQ SP, BP
MOVQ 8(DI), SI // arg 2 sig
MOVQ 0(DI), DI // arg 1 thread
CALL libc_pthread_kill(SB)
POPQ BP
RET

// syscall calls a function in libc on behalf of the syscall package.
// syscall takes a pointer to a struct like:
// struct {
Expand Down
12 changes: 12 additions & 0 deletions src/runtime/sys_darwin_arm.s
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,18 @@ TEXT runtime·pthread_cond_signal_trampoline(SB),NOSPLIT,$0
BL libc_pthread_cond_signal(SB)
RET

TEXT runtime·pthread_self_trampoline(SB),NOSPLIT,$0
MOVW R0, R4 // R4 is callee-save
BL libc_pthread_self(SB)
MOVW R0, 0(R4) // return value
RET

TEXT runtime·pthread_kill_trampoline(SB),NOSPLIT,$0
MOVW 4(R0), R1 // arg 2 sig
MOVW 0(R0), R0 // arg 1 thread
BL libc_pthread_kill(SB)
RET

// syscall calls a function in libc on behalf of the syscall package.
// syscall takes a pointer to a struct like:
// struct {
Expand Down
12 changes: 12 additions & 0 deletions src/runtime/sys_darwin_arm64.s
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,18 @@ TEXT runtime·pthread_cond_signal_trampoline(SB),NOSPLIT,$0
BL libc_pthread_cond_signal(SB)
RET

TEXT runtime·pthread_self_trampoline(SB),NOSPLIT,$0
MOVD R0, R19 // R19 is callee-save
BL libc_pthread_self(SB)
MOVD R0, 0(R19) // return value
RET

TEXT runtime·pthread_kill_trampoline(SB),NOSPLIT,$0
MOVD 8(R0), R1 // arg 2 sig
MOVD 0(R0), R0 // arg 1 thread
BL libc_pthread_kill(SB)
RET

// syscall calls a function in libc on behalf of the syscall package.
// syscall takes a pointer to a struct like:
// struct {
Expand Down

0 comments on commit 42aab4b

Please sign in to comment.