]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: M-targeted signals for libc-based OSes
authorAustin Clements <austin@google.com>
Mon, 14 Oct 2019 19:49:27 +0000 (15:49 -0400)
committerAustin Clements <austin@google.com>
Sat, 26 Oct 2019 02:52:30 +0000 (02:52 +0000)
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>
src/runtime/os2_aix.go
src/runtime/os3_solaris.go
src/runtime/os_aix.go
src/runtime/os_darwin.go
src/runtime/sys_darwin.go
src/runtime/sys_darwin_386.s
src/runtime/sys_darwin_amd64.s
src/runtime/sys_darwin_arm.s
src/runtime/sys_darwin_arm64.s

index 7f69d6d1e37cc445a2dc2bd3c9ebbeb1f3d586df..7c3cb27223202e2ee96c64e69d14803b57a4aa4b 100644 (file)
@@ -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
@@ -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
@@ -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
@@ -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))
+}
index 4ac191fab8eb65061c095ebeac48cfdd739cf220..563e981d0fb10ae4e73cc6f728f522375cc0457e 100644 (file)
@@ -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"
@@ -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
@@ -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,
@@ -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.
@@ -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 */ {
index 855ae6ff469328299466fd81004b4f76d2ea6792..ff2588f42fac006cc733f2eba73acf79cf2d3533 100644 (file)
@@ -175,6 +175,7 @@ func miniterrno() {
 func minit() {
        miniterrno()
        minitSignals()
+       getg().m.procid = uint64(pthread_self())
 }
 
 func unminit() {
index 1614b66c8a76d36222dd2189084258aa0474f051..c11fbec0a585258a2a088e410be240a2d9f1074c 100644 (file)
@@ -295,6 +295,7 @@ func minit() {
                minitSignalStack()
        }
        minitSignalMask()
+       getg().m.procid = uint64(pthread_self())
 }
 
 // Called from dropm to undo the effect of an minit.
@@ -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))
+}
index 46825d59376dfd7496f88bcf949dc12176e8fe60..31304ce737ece3beafd9d9dcdbfb1d9ae7eb1663 100644 (file)
@@ -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
@@ -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"
 
index bea804b8ddff30fb689f52677025a45c78c52bd2..15b7cfb21391ae9755d43ec81f51511378b34e2b 100644 (file)
@@ -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 {
index ea8cf1abb13c31169625af9d701868c999235672..a45ea42e5d91c6ce547bae90aa587ad25c197071 100644 (file)
@@ -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 {
index 84b0b0f5f4ccfe9816110b8cf30409e8de2bc9de..4e201fca09746c9a6b1bdfd1fa86d33e123529a3 100644 (file)
@@ -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 {
index 8d39a0727fc7a11f2015935a6fdc24668ff7aff3..585d4f2c6433fc286fbda546590ebfe951c2101b 100644 (file)
@@ -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 {